This article is from: 103Style blog
The record
General train of thought
- We’ll pass when we get a push message
NotificationManager.notify(int id, Notification notification)
Send to the notification bar. - Log each notification bar message displayed and the corresponding
id
. - When entering the corresponding page according to product requirements
NotificationManager.cancel(id)
Delete the corresponding notification message.
Pseudo code
Through the sendNotification (…). To display the push message, call similar to cleanMsgNotify(int Notice) on the corresponding interface to clear the push message.
public static final String CHANNEL_ID = "XXXX";
private static NotificationManager mNotificationManager;
private static List<PushMessageBean> notifyList;
public synchronized static void cleanMsgNotify(int notice) {
if (mNotificationManager == null
|| notifyList == null || notifyList.size() == 0) {
return;
}
for (int i = notifyList.size() - 1; i >= 0; i--) {
PushMessageBean t = notifyList.get(i);
if (t.notice == notice) {
mNotificationManager.cancel(t.notifyId);
notifyList.remove(i);
}
}
}
public void sendNotification(Context context, PushMessageBean message) {
if (TextUtils.isEmpty(message.message)) {
return;
}
NotificationCompat.Builder mBuilder;
int notifyId = (int) System.currentTimeMillis();
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
registerNotificationChannel();
mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setContentText(message.message)
.setSmallIcon(R.drawable.ic_launchers_round)
.setVibrate(new long[]{1000})
.setColor(context.getResources().getColor(R.color.color_primary))
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message.message));
if(build.version.sdk_int < build.version_codes.n) {// Title is not required above 7.0 mBuilder.setContentTitle(context.getResources().getString(R.string.app_name)); } message.notifyId = notifyId; saveNotification(message); mNotificationManager.notify(notifyId, mBuilder.build()); } private void saveNotification(PushMessageBean message) {if (notifyList == null) {
notifyList = new ArrayList<>();
}
notifyList.add(message);
}
private void registerNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
if (notificationChannel == null) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true); // Whether to display channel.setlightColor (color.red) in the upper right corner of the desktop icon; //channel. SetShowBadge (true); / / in a long time according to the notice of the desktop icon is displayed when the channel mNotificationManager. CreateNotificationChannel (channel); }}}Copy the code
If you like it, please give it a thumbs up.
The above
Scan the qr code below, follow my public account Android1024, click attention, don’t get lost.