Android 8.1上notification的适配

安卓O上的notification适配,参照郭大神的进行学习。

一起来学习Android 8.0系统的通知栏适配吧

下面一篇文章是解决Notification适配的方法。 

解决Android 8.0 的Notification不显示问题

从安卓O开始引入了通知渠道的概念,即我们可以指定一类通知为一个通知渠道,每个渠道的通知互不影响,关闭某一个通道不影响其他通道的通知。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天通知";
            int importance = NotificationManager.IMPORTANCE_MAX;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "subscribe";
            channelName = "订阅通知";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{0, 100, 100, 100});
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }


    public void sendChatMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel("chat");
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                startActivity(intent);
                Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
            }
        }
        Intent intent = new Intent(this, MainActivity.class);
        Uri sound = RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_NOTIFICATION);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天晚上吃点啥?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher_round))
                .setAutoCancel(true)
                // 8.0以前的低版本中,若没有setDefaults,无论多高的优先级,通知都无法弹出横幅
                //.setDefaults(NotificationCompat.DEFAULT_ALL)

                // 这里并非多此一举,channel设置了振动只是为了8.0以上的手机,低版本的振动只能在这里设置
                .setVibrate(new long[]{0, 100, 100, 100})
                .setLights(Color.RED, 1000, 1000)
                .setSound(sound)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1, notification);

    }

    public void sendSubMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条订阅消息")
                .setContentText("收到一条订阅消息")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);
    }

Android 8.1上notification的适配_第1张图片

上面的类别就是我们说的通知渠道。

 

你可能感兴趣的:(android)