No Channel found for pkg=com.example.notificationtest, channelId=null, id=1, tag=null,

android9.0手机进行发送通知的测试,发现通知不能发送成功,报以下错误:

E/NotificationService: No Channel found for pkg=com.example.notificationtest, channelId=null, id=1, tag=null, opPkg=com.example.notificationtest, callingUid=10091, userId=0, incomingUserId=0, notificationUid=10091, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)

原因:由于此条通知没有查找到应用中对应的NotificationChannel的原因,而无法弹出来,查阅资料得知,NotificationChannel是Android O新增的通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道

如果需要发送属于某个自定义渠道的通知,你需要在发送通知前创建自定义通知渠道

示例:

public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(this);
                builder.setContentInfo("content info")
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel("1","my_channel", NotificationManager.IMPORTANCE_DEFAULT);
                    channel.enableLights(true);
                    channel.setLightColor(Color.green(1));
                    channel.setShowBadge(true);
                    manager.createNotificationChannel(channel);
                    builder.setChannelId("1");
                }

                Notification n = builder.build();
                manager.notify(1, n);
                break;
            default:
                break;
        }
    }
发布了14 篇原创文章 · 获赞 5 · 访问量 2610

猜你喜欢

转载自blog.csdn.net/XingTina/article/details/102658408
今日推荐