SoFunction
Updated on 2025-03-11

Common methods of Android message notification Notification (send messages and receive messages)

Notification

#Preface

I have been doing related services for message notification notifications recently, and I will use my spare time to summarize it. It is mainly divided into two parts to record: sending messages and receiving messages.

Send a message

Sending messages is implemented using the notify method of NotificationManager class, and now it is sent in the most common way:

 builder = new (context, channelId);
(.ic_launcher)
                        .setContentTitle("title")
                        .setContentText("content")
Notification notification = ();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(channelId, name,NotificationManager.IMPORTANCE_DEFAULT);
(notificationChannel);
(.app_name,notification);                      

This simple code to send a message is completed.
When sending a message, the builder can carry many parameters. Just check the API for details. Here I will record some common methods I use.

builder..addAction(icon,title,pendingIntent);

The three parameters are int type (api is outdated), charText type and pendingIntent intent.
I think this method is very good. For example, when you need to encapsulate a public pop-up window, but the number of buttons is different from the button color and the title of the pop-up window, you can use this method to specify it. Because action is an array, you need to pass several times. For example, there are two buttons in the pop-up window, then pass two actions. The first parameter passes the color value of the button, the second parameter passes the name of the button, and the third parameter passes the intention of the pendingIntent. In this way, this public pop-up window can be completed without the need for complicated implementation logic.
Similarly, builder supports bundle transfer, the method is as follows:

Bundle bundle = new Bundle();
(bundle);

Bundle can carry parameters. Similarly, the encapsulated public pop-up windows mentioned earlier can also be implemented in the form of bundle passing values, but the way of action will be much more complicated.
Bundle can pass some common parameters, such as a type. When receiving notifications, use type to distinguish what actions to do. This depends on the actual requirements of the project.
Here I put up the jump activity that notifies the jump intention and the code to receive the broadcast, and also make a record for myself.

Bundle bundle = new Bundle();
NotificationChannel notificationChannel = new NotificationChannel("99", "TEMP", NotificationManager.IMPORTANCE_DEFAULT);                 
 builder = new (, "99");
//Intent intent = new Intent(,);
Intent intent = new Intent(,);
(".MY_BROADCAST");
(Intent.CATEGORY_LAUNCHER);
(Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent pendingIntent = (,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = (,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
(.ic_launcher_background)
                        .setContentTitle("title")
                        .setContentText("content")
                        .setWhen(())
                        .addAction(.purple_200,"Sure",pendingIntent)
                        .addAction(,"Cancel",pendingIntent)
                        .setExtras(bundle);
Notification notification = ();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
(notificationChannel);
(.app_name,notification);

Delete notification:

(tag,id);

Note here that sending notifications can be made of two parameters and three parameters. The first parameter of the three parameters is a tag. When calling the deletion method, it must correspond to this tag, that is, it will be transmitted as much as the message you want to delete.

Receive message

When receiving the message, I recreate a class inherited from the NotificationListenerService and then rewrite the several methods I need to use. Since my project is developed by systemmui, it may be slightly different from the actual requirements.

@Override
    public void onNotificationPosted(StatusBarNotification sbn)

This method is a callback when a message notification is received.
I'm not sure why I only sent a notification once, but received two messages in the method. Then find a way to filter it. Use the key and time returned by sbn to judge.

mPreviousNotificationKey = ();
mPreviousNotificationKeyTime = ();

Two variables are created normally to save the key and postTime received by that time. Then use sp to obtain the two values ​​saved last time, and then use sp to store the two values ​​obtained this time.

 String mPreviousNotification = ("notificationKey");
 Long mPreviousNotificationTime = ("notificationTime");
 (new ("notificationKey", mPreviousNotificationKey));
 (new ("notificationTime", mPreviousNotificationKeyTime));

Then judge based on the two values:

if ((mPreviousNotification) && (mPreviousNotificationTime)) {
            return;
}

The values ​​are the same as the two times and directly return to filter out.

 @Override
    public void onNotificationRemoved(StatusBarNotification sbn)

This method is to receive a notification of a delete message.
At this point, it will be convenient for similar needs to be viewed later.

This is the article about Android message notification Notification. For more related Android message notification content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!