适配Android 8.0(Oreo)通知栏行为变更

适配Android 8.0(Oreo)通知栏行为变更

官方文档:https://developer.android.google.cn/about/versions/oreo/android-8.0

行为变更

Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。

如果使用Sdk版本在26以上,使用下面的方式将无法在Android 8.0的手机上显示通知:

Notification notification = new NotificationCompat.Builder(this)
                    .setContentText("崩溃数据上传中-" + crashFile)
                    .setContentTitle("应用异常停止")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .build();
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);

在发布通知之前,你需要创建一个渠道,一个app可以有多个渠道,这些渠道在用户看来就是通知的类别,用户可以选择性打开或关闭这些渠道:
这里写图片描述

用户也可以对每一个渠道的通知行为进行自定义:
这里写图片描述

适配

使用的compileSdk>26才需要进行适配工作。

public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        adaptOreoNotification();
        startNotification(“通知”);
    }

    /**
     * 适配Android 8.0的通知
     */
    private void adaptOreoNotification() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //创建渠道
            NotificationChannel channel = new NotificationChannel("com.newsapp", "NewsAPP-Channel",
                    NotificationManagerCompat.IMPORTANCE_HIGH);
            channel.enableLights(true); //开启闪光灯
            channel.enableVibration(true); //震动
            channel.setDescription("新闻应用的通知,包括应用崩溃、内存泄漏发出的通知。"); //渠道描述
            channel.setLightColor(Color.argb(255, 0, 0, 255));  //颜色

            NotificationChannel channel2 = new NotificationChannel("com.newsapp-2", "NewsAPP-Channel2",
                    NotificationManagerCompat.IMPORTANCE_HIGH);

            //注册渠道
            NotificationManager manager = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
            manager.createNotificationChannel(channel2);
        }
    }

    //开启通知
    private void startNotification(String text) {
            Notification notification = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                notification = new Notification.Builder(this, "com.newsapp")
                        .setContentText(text)
                        .setContentTitle("标题")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
            } else {
                notification = new NotificationCompat.Builder(this)
                        .setContentText(text + crashFile)
                        .setContentTitle("标题")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
            }
            startForeground(1, notification);
        }
}

Android 8.0 发起通知后,app会显示小圆点:
这里写图片描述

其他

如果用户手动关闭了渠道(channel),应用是无法弹出通知的,可以通过以下代码打开渠道设置页:

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,"com.newsapp");
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(intent);

猜你喜欢

转载自blog.csdn.net/mingC0758/article/details/81464268