Android8.0通知栏Notification使用NotificationChannel

代码:

                String myChannelId = "myChannel_01";
                String myChannelName = "myChannel";
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(myChannelId, myChannelName,
                            NotificationManager.IMPORTANCE_LOW);
                    Toast.makeText(this, myChannelId, Toast.LENGTH_SHORT).show();
                    manager.createNotificationChannel(channel);
                    notification = new NotificationCompat.Builder(this, myChannelId)
                            .setContentTitle("我是测试")
                            .setContentText("你有一个新通知")
                            .setWhen(System.currentTimeMillis())
                            .setShowWhen(true)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                            .build();
                }else {
                    notification = new NotificationCompat.Builder(this, myChannelId)
                            .setContentTitle("This is a title")
                            .setContentText("This is a context")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                            .build();
                }
                manager.notify(1, notification);

参考:https://stackoverflow.com/questions/45462666/notificationcompat-builder-deprecated-in-android-o
https://blog.csdn.net/qq_35749683/article/details/80451791

你可能感兴趣的:(Android8.0通知栏Notification使用NotificationChannel)