Android O(8.0) Notification不显示的解决方案

1、Android O(8.0)通知的改变

NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。

Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。比如聊天软件,为每个聊天组设置一个通知渠道,指定特定声音、灯光等配置。

2、Android Channel通知的兼容支配

创建NotificationChannel

  1. 创建NotificationChannel对象,指定Channel的id、name和通知的重要程度
  2. 使用NotificationMannager的createNotificationChannel方法来添加Channel。
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 如果该channel已经存在,则可以不用再次创建
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            //是否绕过请勿打扰模式
            channel.canBypassDnd();
            //闪光灯
            channel.enableLights(true);
            //锁屏显示通知
            channel.setLockscreenVisibility(VISIBILITY_SECRET);
            //闪关灯的灯光颜色
            channel.setLightColor(Color.RED);
            //桌面launcher的消息角标
            channel.canShowBadge();
            //是否允许震动
            channel.enableVibration(true);
            //获取系统通知响铃声音的配置
            channel.getAudioAttributes();
            //获取通知取到组
            channel.getGroup();
            //设置可绕过  请勿打扰模式
            channel.setBypassDnd(true);
            //设置震动模式
            channel.setVibrationPattern(new long[]{100, 100, 200});
            //是否会有灯光
            channel.shouldShowLights();
            // 如果该channel已经存在,则可以不用再次创建
            manager.createNotificationChannel(channel);
        }
        // 注意:此处channel_id必须是一个已存在的channel id;否则无法显示通知
        Notification notification = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle("Title").setContentText("This is content").setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round)).build();
        manager.notify(0, notification);

3、设置通知重要性级别

该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
,如果要支持 Android 7.1(API 25)及以下的设备,还得调用NotificationCompat 的 setPriority 方法来设置,如下所示

builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

Android 8.0 及以上是使用NotificationManager.IMPORTANCE_,Android 7.1 及以下是使用NotificationCompat.PRIORITY_它们都是定义的常量;下面我们以表格的形式更好的展示出来。

用户通知级别Android8.0及以上Android7.0及以下
紧急级别(发出通知声音并显示为提示通知)IMPORTANCE_HIGHPRIORITY_HIGH或者PRIORITY_MAX
高级别(发出通知声音并且通知栏有通知)IMPORTANCE_DEFAULTPRIORITY_DEFAULT
中等级别(没有通知声音但通知栏有通知)IMPORTANCE_LOWPRIORITY_LOW
低级别(没有通知声音也不会出现在状态栏上)IMPORTANCE_MINPRIORITY_MIN

上面这些通知级别用户都是可以在 Channel 设置中更改

4、自定义Channel通知栏

 private void sendCustomNotification() {
        NotificationCompat.Builder builder = getNotificationBuilder();
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_custom_notifition);
        remoteViews.setTextViewText(R.id.notification_title, "custom_title");
        remoteViews.setTextViewText(R.id.notification_content, "custom_content");

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.turn_next, pendingIntent);
        builder.setCustomContentView(remoteViews);
        getNotificationManager().notify(3, builder.build());
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/notification_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通知栏标题"
            android:textSize="24sp"/>

        <TextView
            android:id="@+id/notification_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通知栏内容"/>

    </LinearLayout>

    <Button
        android:id="@+id/turn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="跳转"/>
</RelativeLayout>

5、判断通知是否开启

Api24以上,NotificationManagerCompat中提供了areNotificationsEnabled()方法。该方法中已经对API19以下,API19-24,API24以上,这三种情况做了判断。直接使用其返回值即可。返回true表示打开了消息通知,如果返回false则没有打开。

     private boolean isNotificationEnabled(Context context) {
        boolean isOpened = false;
        try {
            isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isOpened;
    }

6、跳转通知设置界面

如果用户没有开启通知,则可以使用下面的代码跳转到通知设置界面

    private void gotoSet() {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", getPackageName());
            intent.putExtra("app_uid", getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值