Notifications

Material Style Notification

From Google material design documentation:

Notifications inform your app’s users about relevant and timely events in your app. You can create notifications to draw attention to messages from friends, alert a commuter to traffic slowdowns, show the progress of a new app being installed, and more.

Notifications should be synced to all of a user’s devices.

How to add?

I. In your build.gradle file add the latest appcompat library.

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' 
    // X.X.X specify the version
}

II. Get an instance of the NotificationCompat.Builder.

NotificationCompat.Builder builder =  
    new NotificationCompat.Builder(context);

III. Create a Notification using Notification.Builder

Notification notification = builder  
    .setContentTitle("Title")
    .setContentText("This is a notification!")
    .setSmallIcon(R.drawable.ic_notifications_white_small)
    .build();

IV. Show the Notification with the method notify of NotificationManagerCompat with an id of your choice.

NotificationManagerCompat notificationManager =  
    NotificationManagerCompat.from(context);

notificationManager.notify(0x1234, notification);  

Note: Title, text and small icon are mandatory in order to show the notification.

How to style?

Use the method setColor(int color) of NotificationCompat.Builder to set the color of the round background your notification.

Notification notification =  
    new NotificationCompat.Builder(context)
        .setContentTitle("Title")
        .setContentText("This is a notification!")
        .setSmallIcon(R.drawable.ic_bell)
        .setColor(Color.parseColor("#4B8A08"))
        .build();

Notifications with images

Face styled notification

Use the method setLargeIcon(Bitmap) of NotificationCompat.Builder to use a big image next to the small icon.

Notification notification =  
    new NotificationCompat.Builder(context)
        .setContentTitle("Title")
        .setContentText("This is a notification!")
        .setSmallIcon(R.drawable.ic_bell)
        .setLargeIcon(profileImageBitmap) // Bitmap
        .setColor(Color.parseColor("#4B8A08"))
        .build();

Vibrating notifications

Use the method setVibrate of NotificationCompat.Builder to create vibrate patterns accompanying the notification.

long[] vibratePattern = new long[] {  
   millisToWait, millisToVibrate, 
   millisToWait, millisToVibrate
}

Notification notification =  
    new NotificationCompat.Builder(context)
        .setContentTitle("Title")
        .setContentText("This is a notification!")
        .setSmallIcon(R.drawable.ic_bell)
        .setVibrate(vibratePattern)
        .build();

Note: In order to use the vibration you will need to declare the android.permission.VIBRATE in your AndroidManifest.xml file.

Notifications with lights

Use the method setLights(int argb, int msOn, int msOff) of NotificationCompat.Builder to customize the color and the LED pattern shown by the device.

Notification notification =  
   new NotificationCompat.Builder(context)
      .setContentTitle("Title")
      .setContentText("This is a notification!")
      .setSmallIcon(R.drawable.ic_bell)
      .setLights(Color.MAGENTA, onMillis, offMillis)
      .build();

Tips and best practices

I. Notify the user about time-sensitive information directed specifically at them.

II. For notifications sent by another person, include that person's image.

III. When the user touches a notification, enable the user to take immediate action. This may open a detail view, such as a message, or a summary view for multiple notifications.

IV. The round cut is used by default when the setLargeIcon is not used and setSmallIcon method is used. When using the setLargeIcon the circular image has to be done manually.

Compatibility issues

The notifications have different styles in Holo and Material, besides, some features like the tint are not available.

Holo

Material