[Android Getting Started to Project Combat -- 7.2] -- Advanced and Advanced Use of Notifications

        I have learned the basic use of notifications earlier, and now I will learn the advanced and advanced usage of notifications.

1. Play audio when notification comes

       

Notification notification = new NotificationCompat.Builder(MainActivity.this)

                        ..................

                        .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
                        .build();

2. Vibrate when notification comes

        The vibrate attribute is used to set the duration of the mobile phone’s stillness and vibration, in milliseconds, the value with a subscript of 0 indicates the duration of the mobile phone’s stillness, the value with the subscript of 1 indicates the duration of the mobile phone’s vibration, and the value with the subscript of 2 The value in turn represents how long the phone has been stationary. So, if you want the phone to vibrate for 1 second when the notification arrives, then stand still for 1 second, and vibrate again, you can write like this

              Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ............

                        .setVibrate(new long[] {0,1000,1000,1000})
                        .build();

        If you want to use vibration, you also need to declare permissions

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.notificationtest">
    
    <uses-permission android:name="android.permission.VIBRATE"/>

............

3. Set up a big photo

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ..................

                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background)))
                        .build();

4. Set the importance of notifications

        The setPriority() method receives an integer parameter to set the importance of this notification. There are 5 constant values: PRIORITY_DEFAULT indicates the default importance, which has the same effect as not setting it; PRIORITY_MIN indicates the lowest importance, and the system may only Displayed in a specific scenario, such as when the user pulls down the status bar; PRIORITY_LOW indicates a low importance level, and the system may shrink such notifications or change the display order; PRIORITY_HIGH indicates a high importance level, the system may zoom in and rank them relatively close The previous position; PRIORITY_MAX represents the highest level of importance, and this type of notification needs to be seen by the user immediately and respond to the action.

      Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ..............

                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .build();

The effect of the above is as follows:

 

Guess you like

Origin blog.csdn.net/Tir_zhang/article/details/130375862