Android Notification Advanced Usage

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

1. Play Sound When Notification Arrive.

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();

All android built-in ringtones sound files is saved in /system/media/audio/ringtones folders. Please use android device monitor to browse them. If you can not open system folder with click, open a dos window and run below shell command to get android root permission.

adb root

android built-in ringtones sounds file location

2. Make Phone Vibrate When Notification Arrive.

Before you can make you phone vibrate when notification arrive, you need to add below permission in your AndroidManifest.xml file.

<uses-permission android:name="android.permission.VIBRATE" />

Then you can set the phone quite and vibrate duration time ( milliseconds ) in an array. The quite and vibrate time is configured in the long type array one after another. Please see comments in 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.

When there is an unread short message or unreceive phone call, commonly phone LED light will flash when phone screen is locked. You can also set it when notification is arrived 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.

If you want to use default settings for both notification sound, vibrate and LED light, you can use 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.

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

To resolve above issue, you can set 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);

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 up arrow, click the up arrow, the long text will be hidden.

READ :   How To Copy / Paste Clipboard Data In Android

show long notification text in big text style

6. Display Big Picture In Notification.

You can also display a big picture when open the notification by using big picture style.

When you click the down arrow, the picture is displayed, when you click the up arrow, the picture is hidden.

Big picture and big text style can not be used at same time.

android notification big picture example

// 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.

NotificationCompat.Builder’s setPriority() method is used to set notification priority.

// Set this notification priority to max priority.
builder.setPriority(NotificationCompat.PRIORITY_MAX);

There are five notification priority values in NotificationCompat class.

  1. NotificationCompat.PRIORITY_MAX ( == 2) : This priority notification will popup at once when arrived, it will display at screen top without need user click.
  2. NotificationCompat.PRIORITY_HIGH ( == 1): Means this notification is very important, android OS may magnify such notifications or place it first in the notification list.
  3. NotificationCompat.PRIORITY_LOW ( == -1): Not important notification, android OS will place it at the last in the notification list.
  4. NotificationCompat.PRIORITY_MIN ( == -2): The least important notification, android OS will show it only in some special scenario.
  5. NotificationCompat.PRIORITY_DEFAULT ( ==0) : The default notification priority if not specified.
(Visited 350 times, 1 visits today)

Leave a Reply

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.