Testing Android Notification PendingIntent

  • Post author:
  • Post category:Android

So you’ve followed the Creating Status Bar Notifications in the Android DevGuide. You got the notification icon and ticker to appear on the status bar. In case you haven’t, here’s a code snippet.

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.icon, "ticker...", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);
notification.setLatestEventInfo(this, "title...", "text...", pendingIntent);

nm.notify("tag...", 0, notification);

Note the code snippet above is more concise, and also automatically cancels the notification when the users selects it.

So how do you test the PendingIntent in your emulator? Those new to Android development would logically think that clicking on the notification icon in the status bar will call the PendingIntent Activity. However, the notification icon is not clickable. In the Android 2.x emulator, just like in the real mobile device, you would need to drag the statusbar down to see the notifications. You can also do it the long way by clicking the “Menu” button, and it will display the “Notifications” option.

Click on “Notifications” and you will get a list of notifications. Click on the one you created to trigger the Activity.