First Thing is a question in every mind... What is local notification??
Let me tell you what it is actually.
Notification is basically is service to enable an application to inform its users that it has something for them, when application is not running in foreground.
Notification can mainly be of two types.
- Push Notification or Remote Notification
- Local Notification
Local Notification are scheduled by an application and delivered on the same device.
Now you must have got an idea about local notification. You must have thought that all reminder apps use local notification to remind us about any event or schedule.
Now the thing is how to write code in Android to notify user that he/she got a local notification.
Steps are explained below --
- Setting up the Notification
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,message, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, title of notification,message of notification, pendingIntent);
notificationManager.notify(10, notification); // 10 is a random number I chose to act as the id for this notification.
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
======================================================================
This method after combining above two code will look like this.
You need to just call this method in your code whenever and wherever you want.
public void createLocalNotification() {
NotificationManager notificationManager = (NotificationManager) this
- Setting up Pending Intent
A pending intent is a token that you give to another application, which allows this other application to use the permissions of your application to execute a predefined piece of code.
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
This method after combining above two code will look like this.
You need to just call this method in your code whenever and wherever you want.
public void createLocalNotification() {
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher,message, System.currentTimeMillis()); // Here you can set various properties of notification
Intent intent = new Intent(HappyActivity.this, NotificationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// use the flag FLAG_UPDATE_CURRENT to override any notification already
// there
pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher,message, System.currentTimeMillis()); // Here you can set various properties of notification
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, title of notification,message of notification, pendingIntent);
notificationManager.notify(10, notification); // 10 is a random number I chose to act as the id for this notification
}
Thats Done.
Thank You
}
Thats Done.
Thank You
No comments:
Post a Comment