Basic usage and advanced and advanced usage of Android notifications

1. Basic usage

1. A NotificationManager is required to manage notifications.

It can be obtained by calling the getSystemService() method of Context.

Notification managet=(NotificationManager)
getSystemService(Context.NOTIFION_SERVICE):

2. Next, you need to use a Builder constructor to create a Notification object

 
 
Notification notification=new NotificationCompat.Builder(context)
.setContentTitle("This is content title")//used to specify the title content
.setContentText("This is content text")//used to specify the body content
.setWhen(System.currentTimeMillis())//Used to specify the time when the notification is created, in milliseconds.
.setSmallIcon(R.drawble.small_icon)//Used to set the small icon, the small icon will be displayed on the system status bar.
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawble.large_icon))//The large icon used to set the notification. When you pull down the system status bar, you can see the large icons.
.build();//Multiple sets of setting methods can be chained before this method to create a rich Notification

3. After the above work is completed. Just call the notify() method of NotificationManager to display the notification.

manager.notify(1, notification);//The first parameter is the id, to ensure that the id specified by each notification is different. The second parameter is the Notification object

4. Notification click event

·PendingIntent: It can be understood as the Intent of delayed execution.

· Methods to get PendingIntent: getActivity(), getService(), getBroadcast().

     The parameters received by these methods are the same, the first Context, the second usually passes in 0, and the third Intent object. The fourth generally passes in 0.

Construct the required Intent and pass it into the method of getting PendingIntent, and then concatenate setContentIntent(intent) in the statement that constructs the Notification object

Intent intent=new Intent(this,Notification.class);
PendingIntent pi =PendingIntent.getActivity(this,0,intent,0);

5. How to cancel the notification.

1) Concatenate .setAutoCancel(true) in the method of constructing the Notification object;

2) Add in the activity onCreate() of the activity 

NoticationManager manager=(Notification)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);//The id of the notification to be canceled is passed in here

2. Advanced usage of activities

.setSound(Uri.fromFile(new File("/System/media/audio/ringtones/Luna.ogg)))//Set the ringtone
.setVibrate(new long[] {0,1000,1000,1000})//Set the first element of vibration: static duration (ms) and the second: vibration duration (ms) and then static and so on.
.setLights(Color.GREEN,1000,1000)//The first color, the second light time, and the third dark time.
.setDefaults(NotificationCompat.DEFAULT_ALL)//If you don't want to do such a complicated operation. Can be set as the default effect. It will decide what to play based on the environment of the phone.

3. Advanced functions of activities

Rich text content can display long paragraphs of text;

setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to build..............................8***********************.."))
// You can also display large pictures
setStyle(new NotificaitonCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))

Set the importance of the notification.

setPriority()

Parameters and their corresponding properties:

PRIORITY_MIN Minimum importance.

PRIORITY_LOW Lower importance.

PRIORITY_DEFAULT has the same effect as not setting it

PRIORITY_HIGH indicates a higher level of importance, in a higher position, the system will magnify such notifications.

PRIORITY_MAX highest importance will receive zoom notifications regardless of whether they are in a game movie.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325575478&siteId=291194637