android 8.0 报错StartForeground Bad Notification Error 解决方法

2019-08-10 17:47:28  阅读 6166 次 评论 0 条

今天升级了android8.0 并且在一个8.0的系统上运行,居然报错了 

报错日志就是标题这个了 

也就是使用startForeground方法开启通知的时候报错的 

在android8.0后 需要给notification设置一个channelid不要问我为什么 解决方法如下

//新增---------------------------------------------
 String CHANNEL_ONE_ID = "com.primedu.cn";
        String CHANNEL_ONE_NAME = "Channel One";
        NotificationChannel notificationChannel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);
        }
//--------------------------------------------------------新增

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
                .setTicker("Nature")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("xxxx")
                .setContentText(musicList.size() > 0 && musicList != null ? musicList.get(currentMusic).radio_en_desc:"xxxxx")
                .setContentIntent(pendingIntent)
                .getNotification();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(1, notification);
--------------------- 
版权声明:本文为CSDN博主「BigSweetee」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_15527709/article/details/78853048

我标记了一下哪些地方是新增的 

同新增了一句.setChannelId(CHANNEL_ONE_ID) 

就ok了,在运行就没问题了,通知正常开启

--------------------- 

版权声明:本文为CSDN博主「BigSweetee」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_15527709/article/details/78853048


本文地址:http://www.bloguan.com/?id=535
温馨提示:文章内容系作者个人观点,不代表博观网对观点赞同或支持。
版权声明:本文为转载文章,来源于 CSDN-BigSweetee ,版权归原作者所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?