一、启动Notification的简单流程:
1.初始化NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2.创建NotivicationChannel
String mChannelId="channel_id";
String mChannelName="channel_name";
NotificationChannel notificationChannel = new NotificationChannel(mChannelId, mChannelName, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(notificationChannel);
3.创建Notivication实例
Notificationnotification = newNotificationCompat.Builder(this, mChannelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("contenttitle")
.setContentText("contexttext")
.build();
4.调用NotificationManager.notify启动Notification
mNotificationManager.notify(1,notification);
注意事项
1)NotificationChannel
- 渠道控制通知的行为:静音,响铃,振动,闪光等。
- 5个重要性对应不同的默认行为:
Importance | 状态栏 | 通知栏 | 最小化信息 | 静音 | 浮窗 |
---|---|---|---|---|---|
NotificationManager.IMPORTANCE_NONE | × | × | 无 | × | × |
NotificationManager.IMPORTANCE_MIN | × | √ | √ | √ | × |
NotificationManager.IMPORTANCE_LOW | √ | √ | 完全显示 | √ | × |
NotificationManager.IMPORTANCE_DEFAULT | × | √ | 完全显示 | 响铃 | × |
NotificationManager.IMPORTANCE_HIGH | √ | √ | 完全显示 | 响铃 | √ |
2)notify(intid,Notificationnotification)
如果具有相同id的通知,第一个通知如果还没被清除,第二个通知会取代第一个通知。
否则就启动新的通知。
Github 地址
未完待续…