Devoloper warning for package "com..." Failed to post notification on channel "null" see log for det

使用通知时屏幕显示

在这里插入图片描述
这是因为我们使用的是Android O(Android 8.0)之前的通知写法,而Android O之后引入了通知通道(Notification channel)
重新定义通知内容中的应用程序类别,可以让开发者给予用户更精确的通知管理。用户可以阻止或分别更改每个通道的行为,而不是一起管理应用程序的所有通知。

因此我们之前的写法已经不适用Android O(Android 8.0)即以后的版本了


新版通知使用步骤

  1. 创建一个NotificationChannel(通知通道)对象
  2. 使用NotificationManager创建一个通道(通知通道对象为参数)
  3. 编写通知Notification,把通道id传入到第二个参数
  4. 调用NotificationManager的notify()方法发送通知
// 获取NotificationManager对象
NotificationManager manager = 
			(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            String id = "channel_1";
            String name = getString(R.string.app_name);
           
           // 创建NotificationChannel对象,传入id name 和 重要级别
            NotificationChannel notificationChannel =
                    new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);

			// 创建通知的通道
            manager.createNotificationChannel(notificationChannel);

			// 创建通知
			// Builder中传入上下文对象和通道id
            Notification notification = new NotificationCompat
            		.Builder(this, id)
                    .setContentText("hello world")
                    .setContentTitle("This is a test")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            // 发送通知
            manager.notify(1, notification);

至此我们已经完成了Android O(8.0)的通知写法了,运行程序发现我们成功的发送了一条通知。

下面介绍一个通知级别
PRIORITY_DEFAULT 默认的重要程度
PRIORITY_MIN 表示最低的重要程度,系统可能只会在特定的场景才会显示这条通知
PRIORITY_LOW 表示较低的重要程度
PRIORITY_HIGH 表示较高的重要程度,系统可能会将这类通知放大,或改变其显示顺序,比较靠前的位置
PRIORITY_MAX 最高的重要程度,必须让用户立刻看到,甚至做出相应的操作。

在创建通知通道对象时,还可以设置更多的属性

getId()—检索给定通道的ID
enablellights() -如果使用中的设备支持通知灯,则说明此通知通道是否应显示灯
setLightColor() -如果我们确定通道支持通知灯,则允许使用传递一个int值,该值定义通知灯使用的颜色
enablementVisuration()—在设备上显示时,说明来自此通道的通知是否应振动
getImportance()—检索给定通知通道的重要性值
setSound()—提供一个Uri,用于在通知发布到此频道时播放声音
getSound()—检索分配给此通知的声音
setGroup()—设置通知分配到的组
getGroup()—检索通知分配到的组
setBypassDnd()—设置通知是否应绕过“请勿打扰”模式(中断_筛选器_优先级值)
canBypassDnd() -检索通知是否可以绕过“请勿打扰”模式
getName()—检索指定频道的用户可见名称
setLockScreenVisibility() -设置是否应在锁定屏幕上显示来自此通道的通知
getlockscreendisibility() -检索来自此通道的通知是否将显示在锁定屏幕上
getAudioAttributes()—检索已分配给相应通知通道的声音的音频属性
canShowBadge()—检索来自此通道的通知是否能够在启动器应用程序中显示为徽章

大家可以根据我们的需要,自行选择

由于我们的项目开发时要保持严谨,因此我们需要加入对Android版本的判断,在Android O之前的还使用原来的写法,让我们贴出完善的写法

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String id = "channel_1";
            String name = getString(R.string.app_name);
            NotificationChannel notificationChannel =
                    new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);

            manager.createNotificationChannel(notificationChannel);

            Notification notification = new NotificationCompat.Builder(this, id)
                    .setContentText("hello world")
                    .setContentTitle("This is a test")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            manager.notify(1, notification);
        } else {
            Notification notification = new NotificationCompat.Builder(this)
                    .setContentText("hello world")
                    .setContentTitle("This is a test")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            manager.notify(1, notification);
        }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值