android notification manager
notification manager
- Notification manager
It is a class to notify users of event that happen.
- Setting up notifications
By using NotificationManager class via getSystemService() method
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification manager
- Public Methods of Notification Manager
public void cancel(int id)
public void cancel(String tag, int id)
public void cancelAll()
public void notify(int id, Notification notification)
public void notify(String tag, int id, Notification notification)
EXAMPLE:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification);
notification
- Notification class
A class that represents how a persistent notification is to be presented to the user
- Notification Flags
-> FLAG_AUTO_CANCEL
-> FLAG_FOREGROUND_SERVICE
-> FLAG_INSIST
-> FLAG_NO_CLEAR
-> FLAG_ONGOING_EVENT
-> FLAG_ONLY_ALERT_ONCE
-> FLAG_SHOW_LIGHTS
notification builder
- Notification.Builder
Notification.Builder provides a builder interface to create a Notification object.
Notification note = new Notification.Builder(this) .setContentTitle( " New Mail from " + "test@gmail.com") .setContentText("Subject") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .addAction(R.drawable.icon, "Call", pIntent).build();
notification builder
- Commonly used public methods of Notification.Builder
setAutoCancel(boolean autocancel)
setContentTitle(Charsequence title)
setContentText(Charsequence text)
setSmallIcon(int Icon)
setTicker(Charsequence text)
addAction(int icon, Charsequence title, PendingIntent intent)
setContentIntent(Intent)
pending intent
- Pending intent
Pending intent is an intent which is not called immediately.
PendingIntent pendintent = PendingIntent.getActivity(this, 0, intent, 0);
It allows the foreign application to use your application's permissions
to execute a predefined piece of code.
There are three ways to get an instance of Pending Intent:
getService(Context context, int requestCode, Intent intent, int flags)
getActivity(Context context, int requestCode, Intent intent, int flags)
getBroadcast(Context context, int requestCode, Intent intent, int flags)
pending intent
- Pending Intent Flags
The following values are taken as flags in a Pending Intent
FLAG_CANCEL_CURRENT
FLAG_NO_CREATE
FLAG_ONE_SHOT
FLAG_UPDATE_CURRENT
notification Buttons
- Notifications with Buttons
Android allows a maximum of three buttons to a notification
Notification noti = new Notification.Builder(this) .setContentTitle( " New Mail from " + "test@gmail.com") .setContentText("Subject") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .addAction(R.drawable.icon, "Call", pIntent) .addAction(R.drawable.icon, "Message", pIntent) .addAction(R.drawable.icon, "Delete" , pIntent).build();
notification types
- Types of notifications
There are three types of notifications
Notification.BigPictureStyle:
Generates a large format notification including a large image attachment.
Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(BitmapFactory.decodeResource(getResources(),
R.drawable.sunset)).build();
notification types
Notification.BigText
Generates a large-format notification that include a lot of text.
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
notification Types
Notification.InboxStyle
Generates a large-format notification that include a list of (up to 5) strings.
Notification notification = new Notification.InboxStyle(builder)
.addLine("First message").addLine("Second message")
.addLine("Thrid message").addLine("Fourth Message")
.setSummaryText("+2 more").build();
custom views
- Using BigContentView
Developer can put their own custom view to a notification.
Uses BigContentView instead of ContentView of Remoteview in a Notification
RemoteViews bpView = new RemoteViews(this.getPackageName(),
R.layout.notif_boarding_pass);
bpView.setTextViewText(R.id.origin_text_view, origin);
bpView.setTextViewText(R.id.destination_text_view, destination);
Notification notif = new Notification.Builder(this)
.setContentTitle("Boarding pass")
.setContentText("Click for more info")
.setSmallIcon(R.drawable.ic_launcher).build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.bigContentView = bpView;
Thank you
android notification manager
By Torry Harris Business Solutions
android notification manager
- 3,604