catalogue
2.1 get the notification manager first
2.2 create notification channel
2.2. 1 steps to create a notification channel
2.3 create Notification instance object
2.3. 1 to create a notification instance object
3, Advanced tips for notification
4, Advanced features for notifications
4.1. Set some complex things in the prefix
4.2 management notification channel
1, Notice
Notification is literal. You can send a message to the user when the program is closed or running in the background to remind the user. After the user receives the notification, an icon will appear on the top. You can also drop down to view the details. You can click to enter an activity in the app.
Notifications can be created in an activity, in a broadcast receiver, or in a service.
From Android 8 After 0, the use of notifications has changed greatly. Among them, a new thing we must use is the notification channel, NotificationChannel.
2, To create a notification
2.1 get the notification manager first
Notifications are managed by a notification manager. Therefore, before creating or controlling notifications, you need to get a notification manager NotificationManager:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.2 create notification channel
Notification channel is a channel to which each notification belongs. Different types of notifications are classified. This is after android 8.0, so that users can better manage notifications and avoid some rogue notifications (the previous disadvantages are a little similar to the role of runtime permissions).
2.2. 1 steps to create a notification channel
[1] If the sdk of the mobile version is greater than 26, you need to set a notification channel for each notification
[2] Construct the object of the notification channel, and pass in three parameters - notification channel id, notification channel name, and notification channel importance
[3] Call the createNotificationChannel() method of the NotificationManager notification manager, and pass in the parameter ----- notification channel object
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); String channelId = "chat"; String channelName = "Chat message"; int channelImportance = NotificationManager.IMPORTANCE_HIGH; createNotificationChannel(channelId,channelName,channelImportance); channelId = "subscribe"; channelName = "Subscription information"; channelImportance = NotificationManager.IMPORTANCE_DEFAULT; createNotificationChannel(channelId,channelName,channelImportance); } } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId,String channelName,int importance){ NotificationChannel channel = new NotificationChannel(channelId,channelName,importance); NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); }
Among them, we developers only set a default value, and users can change it according to their own needs.
2.3 create Notification instance object
2.3. 1 to create a notification instance object
[1] Construct a notification object
First, you need to construct the Notification object. One of the methods to construct objects -------- > use the new class name Builder (context, Notification channel id). build (). However, note that in order to consider the compatibility of various versions in Android, we do not directly use Notification, but use NotificationCompat to construct the class name here. The context is the current context, and then the Notification channel id is the Notification channel id we created above. Note two important points: the first is to create a pair You can use builder build () to create objects. The second is to consider the problem of adaptation and use the class with compat as a class.
[2] Set some concatenation for the notification object
Then the specific contents of the notice can be found in Set the concatenation from the build () method.
[3] Call the notify method of NotificationManager to display the notification
Finally, call the manager's notify () method.
Notification notification = new NotificationCompat.Builder(this,"chat") .setContentTitle("A session message was received")//Notice title .setContentText("What's for dinner tonight?")//Notice content .setWhen(System.currentTimeMillis())//Set to display when notifications are sent .setSmallIcon(R.drawable.ic_launcher_background)//Small icon for notification .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))//Large icon for notification .setAutoCancel(true)//Used to set whether to cancel automatically after clicking .build();//Finally build a notification manager.notify(1,notification);//Call the notification and pass in the id (unique) of the notification
3, Advanced tips for notification
The advanced technique of notification is mainly to set the click event of notification
3.1 notified click events
PendingIntent needs to be used. PendingIntent is similar to intent, but it is not executed immediately, but at an appropriate time. It can be understood as an intent with delayed execution.
Create a PendingIntent object. This class has three static methods to construct this object: getActivity(), getService(), getBroadCast(); see the code for the passed parameters:
Then set a concatenation for the notification object setContentIntent() is OK, so you will jump after clicking the notification.
Intent intent = new Intent(this, com.example.notificationtest.NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0); //............ .setContentIntent(pi)
4, Advanced features for notifications
The first is to set up more complex links; The second is to manage the notification channel - reading the notification channel requires opening or deleting the notification channel
4.1. Set some complex things in the prefix
//[6 set rich text standard chart] // . setStyle(new NotificationCompat.BigTextStyle().bigText("call setStyle() method, required in the parameter"+ // "Pass a BigTextStyle object, build one by yourself, then call the bigText() method of the object, inside" + " // .setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(BitmapFactory. decodeResource(getResources(),R.drawable.ic_launcher_foreground)))
You can also set up notifications such as vibration and flash
4.2 management notification channel
First of all, you should know that after the channel is created, it cannot be modified through code, such as id and name. To this end, Android gives developers the permission to read the notification channel configuration. We can tell users to perform relevant operations through the channel status (on, off). Here is a case to remind users to open a channel:
public void sendChatMsg(View view){ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //[3.1.5 episode] if this is a very important notification channel, but the user turns it off, you need to guide the user to turn it on, which will be available after 8 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //First, judge whether it is closed if (manager.getNotificationChannel("chat").getImportance()== Notification.BADGE_ICON_NONE){ //Create an intention to represent that we are going to perform this action - > jump to an activity Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); //This intention carries data to the activity to be opened for acquisition, indicating which app is on the notification setting page we want to open //And which notification channel system will obtain these two data intent.putExtra(Settings.EXTRA_APP_PACKAGE,getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID,"chat"); startActivity(intent); Toast.makeText(this, "Please open notification permission", Toast.LENGTH_SHORT).show(); } }
In addition to the above methods of managing notification channels, Android 8.0 also gives us the function of deleting notification channels by using the following code:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.deleteNotificationChannel(channelId);
The above is Android 8 For the use of Notification above 0, refer to: https://blog.csdn.net/guolin_blog/article/details/79854070