Build Notifications In Android Wear

In this article, I will discussing with you how to build Notifications in Android Wear.

Introduction

Notifications are generally common in most of the wearable applications. As a developer, one has to understand how to build notifications.

The following combination of classes are used for building notifications: 

  1. Notification  
  2. NotificationCompact.Action  
  3. NotificationCompat.Builder  
  4. NotificationCompat.WearableExtender  
  5. NotificationManagerCompat  
If you are an Android programmer, you should be already knowing how to create notifications and use them in your application.

The notifications in wearables are designed in a similar fashion but there is something called stacked notification that you see only on wearables. Stacked notifications are nothing but a group of notifications which are stacked together and is shown as a single card. It lets the user to choose and read all notifications.

Please take a look into the following link to have some understanding about Android Wear

Example 1: Constructing a Notification instance.

The following is the code sample for constructing a simple Notification instance. We are yet to display the notification which you will be learning next.
  1. Notification notification = new NotificationCompat.Builder(this)  
  2.     .setSmallIcon(R.mipmap.ic_launcher)  
  3.     .setContentTitle("CP notification")  
  4.     .setContentText("CodeProject wear notification!")  
  5.     .build();  
Example 2: Extending Notification.

In this example, we will be adding a wearable feature to notification by extending the notification instance to have WearableExtender instance.

You may set any of the available properties of WearableExtender as you wish. Finally it has to be attached to the Notification instance by making a call to the “extend” method and passing in theWearableExtender instance. Also in this example, you can see below a NotificationManagerCompat instance is being used to create a wearable notification.

Code Info

Please note – The following code snippet is part of open source sample.
  1. NotificationCompat.WearableExtender wearableExtender =  
  2.     new NotificationCompat.WearableExtender()  
  3.             .setHintShowBackgroundOnly(true);  
  4.   
  5. Notification notification =  
  6.     new NotificationCompat.Builder(this)  
  7.             .setSmallIcon(R.drawable.ic_launcher)  
  8.             .setContentTitle("Hello Android Wear")  
  9.             .setContentText("First Wearable notification.")  
  10.             .extend(wearableExtender)  
  11.             .build();  
  12.   
  13. NotificationManagerCompat notificationManager =  
  14.     NotificationManagerCompat.from(this);  
  15.   
  16. int notificationId = 1;  
  17.   
  18. notificationManager.notify(notificationId, notification);  
Extending Notification

Example 3: Stacked Notification.

In this example, we will see how to build a stacked notification. The following is the code snippet that shows how to display stacked notifications.

You can see below, you can have two or more Notification instances and a NotificationManagerCompat instance is used for displaying grouped notifications. In order for the stacked notifications to be shown together, we have to group the Notification and that’s done by setting the same group key to all the Notification instance which you are interested in. 
  1. public void DisplayStackedNotifications() {  
  2.   
  3.     final String GROUP_KEY = "group_messages";  
  4.   
  5.     Notification notification1 = new NotificationCompat.Builder(this)  
  6.             .setContentTitle("Message from User1")  
  7.             .setContentText("What's for lunch? "  
  8.                     + "Can we have a veggie burger?")  
  9.             .setSmallIcon(R.mipmap.ic_launcher)  
  10.             .setGroup(GROUP_KEY)  
  11.             .build();  
  12.   
  13.     Notification notification2 = new NotificationCompat.Builder(this)  
  14.             .setContentTitle("Message from CodeProject")  
  15.             .setContentText("How is your article writing going?")  
  16.             .setSmallIcon(R.mipmap.ic_launcher)  
  17.             .setGroup(GROUP_KEY)  
  18.             .build();  
  19.   
  20.     NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);  
  21.   
  22.     notificationManager.notify(1, notification1);  
  23.     notificationManager.notify(2, notification2);  
  24. }  
Stacked Notification
Example 4: Adding action(s) to notification.

In this example, let us see how we can add actions to notifications. There are times, you wish to not only show notifications but if you are also interested in including some of the actions that should be triggered, say you wish to open a new activity or do some other interesting things, then you should be adding actions.

There is something called primary actions on wearables, you can set a content action on NotificationCompat.Builder by making a call to setContentIntent.

Code Info

Please Note – The code snippet is based on How exactly to use Notification.Builder.
  1. public void NotificationWithPrimaryIntent()  
  2. {  
  3.     int notificationID = 0;  
  4.     NotificationCompat.Builder builder = new NotificationCompat.Builder(this)  
  5.                     .setSmallIcon(R.mipmap.ic_launcher)  
  6.                     .setContentTitle("Notification")  
  7.                     .setContentText("Test notification with Content Intent");  
  8.   
  9.     Intent notificationIntent = new Intent(this, MyDisplayActivity.class);  
  10.     PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent,  
  11.             PendingIntent.FLAG_UPDATE_CURRENT);  
  12.     builder.setContentIntent(contentIntent);  
  13.   
  14.     NotificationManager notificationManager =   
  15.                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  16.     notificationManager.notify(notificationID, builder.build());  
  17. }  
open

Let us see how to add custom action to notification. The following is the code snippet for the same. Here’s what we do. 
  1. We are creating an Intent instance with an action ACTION_DIAL to dial the wearable connected phone.

  2. A PendingIntent instance is created to use the callPhoneIntent. Next, we are building a NotificationCompat.Action with an icon, text and the pending intent.

  3. NotificationCompat.Builder instance is created with an icon, text, title. Also you will notice, there is an action being added by making a call to addAction passing in theNotificationCompat.Action instance.

  4. At last, a NotificationManager instance is created to notify the user by making a call to “notify” method with the notification id and NotificationCompat.Builder instance. 
  1. public void NotificationWithAction()  
  2. {  
  3.     int notificationID = 0;  
  4.     Intent callPhoneIntent = new Intent(Intent.ACTION_DIAL);  
  5.   
  6.     PendingIntent callPhonePendingIntent = PendingIntent.getActivity(this0,  
  7.             callPhoneIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  8.   
  9.     NotificationCompat.Action callPhoneAction =  
  10.             new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Call Phone",  
  11.                                                   callPhonePendingIntent)  
  12.             .build();  
  13.   
  14.     NotificationCompat.Builder builder =  
  15.             new NotificationCompat.Builder(this)  
  16.                     .setSmallIcon(R.mipmap.ic_launcher)  
  17.                     .setContentTitle("Notification")  
  18.                     .setContentText("Call Phone")  
  19.                     .addAction(callPhoneAction);  
  20.   
  21.     NotificationManager notificationManager =   
  22.                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  23.     notificationManager.notify(notificationID, builder.build());  
  24. }  
Notification
Example 5: Notification through broadcast receiver

In this example, we will try to understand how to display notification within the broadcast receiver. We need an Intent instance to trigger or send a broadcast. Also the BroadcastReceiver is responsible for receiving the broadcast message.

We will be modifying the AndroidManifest.xml file to include the broadcast receiver with an intent filter action as com.example.ranjan.wearablenotification.SHOW_NOTIFICATION. It is very important to add this change else the broadcast receiver won’t be able to receive anything.
  1. <receiver  
  2.     android:name=".MyPostNotificationReceiver"  
  3.     android:exported="true" >  
  4.     <intent-filter>  
  5.         <action android:name="com.example.ranjan.wearablenotification.SHOW_NOTIFICATION" />  
  6.     </intent-filter>  
  7. </receiver>  
The following is the code snippet of the broadcast receiver. We are overriding the onReceive method and implementing the code for notification. You can notice below a Notification Instance is created with an icon, title, etc. Then we use a NotificationManager instance to notify the same.
  1. public class MyPostNotificationReceiver extends BroadcastReceiver {  
  2.     public static final String CONTENT_KEY = "contentText";  
  3.   
  4.     public MyPostNotificationReceiver() {  
  5.     }  
  6.   
  7.     @Override  
  8.     public void onReceive(Context context, Intent intent) {  
  9.         Intent displayIntent = new Intent(context, MyDisplayActivity.class);  
  10.         String text = intent.getStringExtra(CONTENT_KEY);  
  11.   
  12.         Notification notification = new Notification.Builder(context)  
  13.                 .setSmallIcon(R.mipmap.ic_launcher)  
  14.                 .setContentTitle(text)  
  15.                 .extend(new Notification.WearableExtender()  
  16.                         .setDisplayIntent(PendingIntent.getActivity(context, 0, displayIntent,  
  17.                                 PendingIntent.FLAG_UPDATE_CURRENT)))  
  18.                 .build();  
  19.   
  20.         ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))  
  21.               .notify(0, notification);  
  22.   
  23.         Toast.makeText(context, context.getString(R.string.notification_posted),  
  24.                       Toast.LENGTH_SHORT).show();  
  25.     }  
  26. }  
The following is the code snippet for sending broadcast message. We are creating an Intent instance and set an action same as we set within the intent-filter. The notification text is set by making a call to putExtra with a key as CONTENT_KEY and value from R.string.title.
  1. public class MyStubBroadcastActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.   
  6.         Intent intent = new Intent();  
  7.         intent.setAction("com.example.ranjan.wearablenotification.SHOW_NOTIFICATION");  
  8.         intent.putExtra(MyPostNotificationReceiver.CONTENT_KEY, getString(R.string.title));  
  9.         sendBroadcast(intent);  
  10.         finish();  
  11.     }  
  12. }