Tuesday, August 26, 2014

Notifications

For messages that are important, you should use a more persistent method by using the NotificationManager.

NotificationManager displays a persistent message at the top of the device, commonly known as the status bar/notification bar.

Notifications are handled by the Notification Manager, and currently have the ability to
  • Create new status bar icons
  • 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 audio)

Introducing the Notification Manager

The Notification Manager is a system Service used to handle Notifications

String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager;
notificationManager = (NotificationManager)getSystemService(svcName);

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Using the Notification Manager you can
  • trigger new Notifications
  • modify existing ones
  • remove those that are no longer required

Creating Notifications

Android offers a number of ways to convey information to users using Notifications.
1. The status bar icon
2. The extended notification status drawer
3. Additional phone effects such as sound and vibration

Creating a Notification and Configuring the Status Bar Icon

// Choose a drawable to display as the status bar icon
int icon = R.drawable.icon;
// Text to display in the status bar when the notification is launched
String tickerText = "Notification";
// The extended status bar orders notification in time order
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

Set the Notification object’s number property to display the number of events a status bar icon represents

Setting this value to a number greater than 1, overlays the values as a small number over the status bar icon

notification.number++;

Configuring the Extended Status Notification Display

can configure the appearance of the Notification within the extended status window in two ways:
1. Use the setLatestEventInfo method to update the details displayed in the standard extended
status Notification display.
2. Set the contentView and contentIntent properties to assign a custom UI for the extended
status display using a Remote View.

Triggering a notification

To fire a Notification, pass it in to the notify method on the NotificationManager along with an integer reference ID

int notificationID = 1;
notificationManager.notify(notificationID, notification);

To update a Notification that’s already been fired, re-trigger it using the Notification Manager, passing the notify method the same reference ID

Canceling a notification

Canceling a Notification removes its status bar icon and clears it from the extended status window

You also use the reference ID to cancel Notifications by calling the cancel method on the Notification
Manager

notificationManager.cancel(notificationID);

Example


public class MyActivity extends Activity
{
    public void onClick(View view) {
        displayNotification();
    }
    
    protected void displayNotification()
    {
        //---PendingIntent to launch activity if the user selects this notification---
        Intent i = new Intent(this, NotificationView.class);
        i.putExtra("notificationID", notificationID);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

//The message shown in the status bar is provided to the constructor         Notification notif = new Notification(R.drawable.ic_launcher, "Reminder: Meeting starts in 5 minutes", System.currentTimeMillis());

        CharSequence from = "System Alarm";
        CharSequence message = "Meeting with customer at 3pm...";
        
//Set the details of the notification using setLatestEventInfo method on the Notification instance. The details would show up when the status bar is swiped down. Also pass a pending intent that has to be invoked when the notification is tapped
        notif.setLatestEventInfo(this, from, message, pendingIntent);

        //---100ms delay, vibrate for 250ms, pause for 100 ms and
        // then vibrate for 500ms---
        notif.vibrate = new long[] { 100, 250, 100, 500};
        nm.notify(notificationID, notif);        
    }
}

public class OnNotificationClickActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
            
        //---look up the notification manager service---
        NotificationManager nm = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);
        
        //Get the notificationID of the notification to cancel
        int notificationID = getIntent().getExtras().getInt("notificationID")
                 
        //---cancel the notification that we started--- 
        nm.cancel(notificationID);
    }
}

No comments:

Post a Comment