android 9适配通知栏

最近安装了一个9.0的模拟器,发现通知栏不显示,也没有任何打印日志,把过滤条件改成“No Filters”就可以看到

2018-11-07 14:52:03.987 1908-1992/? E/NotificationService: No Channel found for pkg=com.dahai.floatnotes, channelId=id, id=1, tag=null, opPkg=com.dahai.floatnotes, callingUid=10087, userId=0, incomingUserId=0, notificationUid=10087, notification=Notification(channel=id pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 color=0xff008577 category=reminder vis=PRIVATE)

以前设置通知的代码,在8.0没有问题

        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(getApplicationContext(), "packageName");
        Notification notification = notificationCompatBuilder
                // Title for API <16 (4.0 and below) devices.
                .setContentTitle("标题")
                // Content for API <24 (7.0 and below) devices.
                .setContentText("内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(
                        getResources(),
                        R.mipmap.ic_logo))
                .setContentIntent(notifyPendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                .setCategory(Notification.CATEGORY_REMINDER)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
        NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);

查看了官方文档介绍找了很久才找到如何解决官方描述
现在在设置渠道的时候需要设置到系统中去

    public static String createNotificationChannel(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "channelId";
            CharSequence channelName = "channelName";
            String channelDescription ="channelDescription";
            int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
            // 设置描述 最长30字符
            notificationChannel.setDescription(channelDescription);
            // 该渠道的通知是否使用震动
            notificationChannel.enableVibration(true);
            // 设置显示模式
            notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);

            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            return channelId;
        } else {
            return null;
        }
    }
        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(getApplicationContext(), createNotificationChannel(mContext));
        Notification notification = notificationCompatBuilder
                // Title for API <16 (4.0 and below) devices.
                .setContentTitle("标题")
                // Content for API <24 (7.0 and below) devices.
                .setContentText("内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(
                        getResources(),
                        R.mipmap.ic_logo))
                .setContentIntent(notifyPendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                .setCategory(Notification.CATEGORY_REMINDER)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
        NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);

这样就能显示出来了

还有一个问题startForeground
这里也要传入一个通知,如果不错适配会直接报错

    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=id pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0xff008577 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1737)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

该问题的解决方法和通知栏一样,不过还要加一个权限 该权限是普通权限,可以直接添加

推荐一款应用悬浮笔记,以上问题都在这个APP中出现过并已解决

你可能感兴趣的:(Android)