Thursday, May 9, 2013

Customise Basic Push Notifications on Urban Airship Android

For this tutorial, I modified the sample application code inside the Urban Airship package. To be able to follow, you must have the Urban Airship Push Sample application working on your side (have the urbanairship.properties file ready) and have the android-support-v4.jar properly setup (used for building the custom notification for the app).

From the sample app, you will notice that the basic push notification layout looks exactly the same for all your push alerts. It is sufficient if you want to make all your notifications standard but sometimes, you want to make differences for your alerts to help the user have a better idea of the type of message received (for example some notifications can be considered more important than others).

To start, you need to modify the MyApplication class. Previously, the code to customize the layout is this block:

1:  CustomPushNotificationBuilder nb = new CustomPushNotificationBuilder();  
2:  nb.statusBarIconDrawableId = R.drawable.icon_small;//custom status bar icon  
3:  nb.layout = R.layout.notification;  
4:  nb.layoutIconDrawableId = R.drawable.icon;//custom layout icon  
5:  nb.layoutIconId = R.id.icon;  
6:  nb.layoutSubjectId = R.id.subject;  
7:  nb.layoutMessageId = R.id.message;  
8:  PushManager.shared().setNotificationBuilder(nb);  

You can replace the code in that block with:

1:  BasicPushNotificationBuilder nb = new BasicPushNotificationBuilder() {  
2:       @Override  
3:       public Notification buildNotification(String alert,   
4:            Map<String, String> extras)   
5:       {  
6:            return null;  
7:       }  
8:  };  
9:  PushManager.shared().setNotificationBuilder(nb);  

The code above will prevent the push manager from handling the creation of the notification.

Going back to the MyApplication class, since we already have the IntentReceiver class as the handler for the intents from the push manager,

1:  PushManager.shared().setIntentReceiver(IntentReceiver.class);  

we can go to this class and start modifying it to handle the notification creation.

First, add a NotificationManager class instance to handle the sending of our custom notification. On my sample application, I placed it within the onReceive method:

1:      if (notificationManager == null) {  
2:           notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  

Then, find the block that handles the action for a push receive. On the push sample code, it is in here:

1:  if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {  
2:  }  

Add the code to create and send the Notification: (for this sample application, I am expecting the extra data value from the push to appear on this form: notif_type,notif_id,title,message and with key notif_data )

1:  if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {  
2:       String notif_test = intent.getStringExtra("notif_data");  
3:       if (notif_test != null) {  
4:             String[] notif_data = notif_test.split(",");  
5:             Uri ringToneUri = RingtoneManager.getDefaultUri((notif_data[0].equalsIgnoreCase("0")) ? RingtoneManager.TYPE_NOTIFICATION : RingtoneManager.TYPE_RINGTONE);  
6:             int notif_icon = (notif_data[0].equalsIgnoreCase("0")) ? android.R.drawable.ic_dialog_alert : android.R.drawable.ic_dialog_info;  
7:             NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)  
8:             .setSound(ringToneUri)  
9:             .setSmallIcon(notif_icon)  
10:             .setContentTitle(notif_data[2])  
11:                      .setContentText(notif_data[3])  
12:                      .setAutoCancel(true)  
13:                      .setStyle(new NotificationCompat.InboxStyle());  
14:             Notification notification = mBuilder.build();  
15:             notificationManager.notify(Integer.parseInt(notif_data[1]), notification);  
16:       }  
17:  }   

When sending notifications, you can set the first part of the extra value (notif_type) to 0 or any number to change the icon and the tone played for the notification.

To view the complete source for my test application go here.

2 comments:

  1. Nice post with helpful details. Thanks for sharing. I really appreciate your job.

    emergency notification

    ReplyDelete