[Tutorial] Push Notification

  • Replies:0
  • OpenNot stickiedAnswered

Apr 8, 2013 8:20:01 AM via Website

This tutorial is provided by 9android developers. Full source and download at http://www.9android.net/push-notification/
A user notification framework. Notifications let you signal users without steal-ing focus or interrupting their current Activities. They’re the preferred technique for getting a user’s attention from within a Service or Broadcast Receiver. For example, when a device receives a text message or an incoming call, it alerts you by flashing lights, making sounds, displaying icons, or showing dialog messages. Notifications are a way for your applications to alert users, without using an Activity. Notifications are handled by the NotificationManager, and currently include the ability to:
+ Create a new status bar icon.
+ Display additional information (and launch an Intent) in the extended status bar window.
+ Flash the lights/LEDs.
+ Vibrate the phone.
+ Sound audible alerts (ringtones, media store sounds)

Create Notification
2.1. PendingIntent

A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent so get a PendingIntent via the getBroadcast() method of thePendingIntent class. To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
1//Intent to launch when click on notification.
2 Intent notificationIntent = new Intent(this, MainActivity.class);
3 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);

2.2. Create Notification Method

Create a notification in Lastest Event Layout

1private void pushLastestEventNotification() {
2 String ns = Context.NOTIFICATION_SERVICE;
3 mNotificationManager = (NotificationManager) getSystemService(ns);
4 int icon = R.drawable.icon;
5 CharSequence tickerText = "Push Notification Tutorial";
6 long when = System.currentTimeMillis();
7 // Create new Notification objects
8 Notification notification = new Notification(icon, tickerText, when);
9 // Setting up for Notification fields.
10 notification.flags = Notification.FLAG_AUTO_CANCEL;
11 notification.number += 1;
12 // Create a pending intent to launch to another application when click
13 // on notification.
14 Context context = getApplicationContext();
15 Intent notificationIntent = new Intent(this, ResultActivity.class);
16 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
17 notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
18 // Sets the contentView field to be a view with the standard
19 // "Latest Event" layout.Uses the icon and when fields to set the icon
20 // and time fields in the view.
21 notification.setLatestEventInfo(context, "ContentTitle-9Android.net",
22 "ContentText-Push Notification Tutorial", contentIntent);
23 mNotificationManager.notify(NOTIFICATION_ID1, notification);
24
25 }



Create a notification in custom layout

1private void pushCustomLayoutNotification() {
2 String ns = Context.NOTIFICATION_SERVICE;
3 mNotificationManager = (NotificationManager) getSystemService(ns);
4 int icon = R.drawable.icon;
5 CharSequence tickerText = "Push Notification Tutorial";
6 long when = System.currentTimeMillis();
7 // Create new Notification objects
8 Notification notification = new Notification(icon, tickerText, when);
9 // Setting up for Notification fields.
10 notification.flags = Notification.FLAG_AUTO_CANCEL;
11 notification.number += 1;
12 // Create a pending intent to launch to another application when click
13 // on notification.
14 Context context = getApplicationContext();
15 Intent notificationIntent = new Intent(this, ResultActivity.class);
16 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
17 notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
18 // Inflate custom layout to Remote View of Notification object.
19 RemoteViews contentView = new RemoteViews(getPackageName(),
20 R.layout.notification_layout);
21 contentView.setImageViewResource(R.id.notification_layout_icon_img,
22 R.drawable.icon);
23 contentView.setTextViewText(R.id.notification_layout_text,
24 "Notification Tutorial of 9Android.net");
25 notification.contentView = contentView;
26 notification.contentIntent = contentIntent;
27 mNotificationManager.notify(NOTIFICATION_ID2, notification);
28
29 }

— modified on Apr 15, 2013 7:01:35 AM