Android Notification Advanced Usage

Android Custom Notification Example has told you the basic operation of android notification management. This article will show you some advanced features of android notifications.

1. Play Sound When Notification Arrive.

  1. You can set which sound to play when you receive an android notification like below.
    // Get notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this);
    
    // Set sound file uri. You can also use your own sound file.
    Uri soundUri = Uri.parse("/system/media/audio/ringtones/Andromeda.ogg");
    builder.setSound(soundUri);
    
    // Create notification object.
    Notification notification = builder.build();
  2. All android built-in ringtones sound files are saved in /system/media/audio/ringtones folders. Please use the android device monitor to browse them. If you can not open the system folder with a click, open a dos window and run the below shell command to get android root permission.
    adb root

2. Make Phone Vibrate When Notification Arrive.

  1. Before you can make your phone vibrate when a notification arrives, you need to add the below permission in your AndroidManifest.xml file.
    <uses-permission android:name="android.permission.VIBRATE" />
  2. Then you can set the phone quiet and vibrate duration time ( milliseconds ) in an array. The quiet and vibrate time is configured in the long type array one after another. Please see the comments in the below code.
    // Get notification builder. 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this); 
    
     // Specify the phone quite and vibrate duration time ( in milliseconds ) in an array.
     // The first time in this array is the quite time when notification arrive.
     // The second time in this array is the vibrate time after first quite time.
     // The third time in this array is the quite time again after previous vibrate and so on.
     // Phone quite time and vibrate time is
     long vibrateTimeArr[] = {1000, 2000, 1000, 2000, 1000};
     builder.setVibrate(vibrateTimeArr);
    
    // Create notification object. 
    Notification notification = builder.build();

3. Make Phone LED Light Flash When Notification Arrive And Screen Is Locked.

  1. When there is an unread short message or unreceived phone call, commonly phone LED light will flash when the phone screen is locked.
  2. You can also set it when a notification arrives while the phone screen is locked.
    // Get notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this);
    
    /* The first parameter is the LED light color.
    *  The second parameter is the LED light turn on duration time( milliseconds ).
    *  The third parameter is the LED light turn off duration time( milliseconds )*/
    builder.setLights(Color.RED, 1000, 1000);
    
    // Create notification object.
    Notification notification = builder.build();

4. Use Android System Default Sounds, Vibrate, And LED Light Settings For Notification.

  1. If you want to use default settings for both notification sound, vibrate, and LED light, you can use the below code.
    // Get notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this);
    
    // Use all default settings.
    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
    
    // Only use default sound and vibrate settings.
    builder.setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE);
    
    // Only use default LED light settings.
    builder.setDefaults(NotificationCompat.DEFAULT_LIGHTS);
    
    // Create notification object.
    Notification notification = builder.build();

5. Show Long Text In Notification.

  1. If the notification message text is too long, then the message text can not be displayed completely in the notification. There are some ellipsis characters at the end of the notification.
    // Get notification builder. 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this); 
    
    // Create long notification text string. 
    String longNotificationText = "Hello, this is the large screen size notification example. The text is long, so it can not be displayed completely, you can only see parts of the message.";
      
    // Set notification text as normal.
    builder.setContentText(longNotificationText);

    android-long-content-text-notification-without-big-text-style-settings

  2. To resolve the above issue, you can set a big text style for the notification style as below.
    // Get notification builder. 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this); 
    
    String longNotificationText = "Hello, this is the large screen size notification example. The text is long, so it can not be displayed completely, you can only see parts of the message.";
    
    // Create a BigTextStyle object.
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.bigText(longNotificationText);
    bigTextStyle.setBigContentTitle("Happy Christmas Detail Info.");
    
    // Set big text style.
    builder.setStyle(bigTextStyle);
  3. Then you can see there is a down arrow at top of the notification, click it will display complete content text for the notification. The arrow will also change to an up arrow, click the up arrow, the long text will be hidden.
    show-long-notification-text-in-big-text-style

6. Display Big Picture In Notification.

  1. You can also display a big picture when open the notification by using the big picture style.
  2. When you click the down arrow, the picture is displayed, when you click the up arrow, the picture is hidden.
    android-notification-big-picture-example
  3. The big picture and big text styles can not be used at the same time.
    // Get notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this);
    
    // Create new big picture style.
    NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
    
    // Create big picture bitmap object from a drawable resource.
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.big_picture);
    
    // Set bitmap.
    bigPictureStyle.bigPicture(bitmap);
    
    // Use big picture style.
    builder.setStyle(bigPictureStyle);
    
    // Create notification object.
    Notification notification = builder.build();

7. Set Notification Priority.

  1. NotificationCompat.Builder‘s setPriority() method is used to set notification priority.
    // Set this notification priority to max priority.
    builder.setPriority(NotificationCompat.PRIORITY_MAX);
  2. There are five notification priority values in NotificationCompat class.
  3. NotificationCompat.PRIORITY_MAX ( == 2): This priority notification will pop up at once when arrived, it will display at screen top without need the user click.
  4. NotificationCompat.PRIORITY_HIGH ( == 1): This means this notification is very important, android OS may magnify such notifications or place it first in the notification list.
  5. NotificationCompat.PRIORITY_LOW ( == -1): Not important notification, android OS will place it at the last in the notification list.
  6. NotificationCompat.PRIORITY_MIN ( == -2): The least important notification, android OS will show it only in some special scenario.
  7. NotificationCompat.PRIORITY_DEFAULT ( ==0) : The default notification priority if not specified.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.