基本使用(两种方式)
NotificationManager notificationManager = getSystemService(NotificationManager.class);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
NotificationChannel channel =new NotificationChannel("1001",
"test_channel",NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("for test channel description");
notificationManager.createNotificationChannel(channel);
NotificationChannelCompat channelCompat =new NotificationChannelCompat.Builder("1001",
NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setDescription("for test compat channel description")
.build();
notificationManagerCompat.createNotificationChannel(channelCompat);
Intent intent = new Intent(MainActivity.this,ChildActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,1001,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this, "1001")
.setSmallIcon(R.drawable.search)
.setContentTitle("title")
.setContentInfo("body")
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(1,notification);
Notification notificationCompat = new NotificationCompat.Builder(MainActivity.this, "1001")
.setSmallIcon(R.drawable.search)
.setContentTitle("title")
.setContentInfo("body")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.build();
notificationManagerCompat.notify(1,notificationCompat);
notificationManager.cancel(1);