- Get link
- X
- Other Apps
Setting up Notifications
Notifications in Android are represented by the Notification class. To create notifications you use the NotificationManager class which can be received from the Context, e.g. an activity or a service, via the getSystemService() method.
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 get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
The code would look something like this:
CharSequence from = "Program Name";
CharSequence message = "You have a message!" ;
NotificationManager nm = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent
.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
message, System.currentTimeMillis());
notif.defaults=Notification.DEFAULT_ALL;
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledARGB = 0xff00ff00;
notif.ledOnMS = 300;
notif.ledOffMS = 1000;
notif.setLatestEventInfo(this, from, message, contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
You can customize the notification by setting properties and methods of your Notification object.
Canceling Notifications
The user can dismiss all notification or if you set your notification to auto-cancel it is also removed once the user selects it.
You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued.
- Get link
- X
- Other Apps
Comments
Post a Comment