Friday, 7 August 2015

Notification with Icon and Sound

Hello Guys,

Here I am going to explain what is Notification and how to use it in your ANDROID applications.

Definition of Notification: Its nothing but an alert which display on your device statusbar with different symbols.

For Example: If you will get an alert of Gmail then it shows icon of gmail on your device top left corner. This alert call notification.

If you want to create a notification in android then you must have use Notification Manager with its Notification Service like following line of code.

NotificationManager notificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

Sample Code:

               // define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri 
                (RingtoneManager. TYPE_NOTIFICATION);

// intent triggered, you can add other intent for other actions
Intent intent = new Intent(NotificationActivity.this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);

// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(this)
.setContentTitle("New Post!")
.setContentText("Here's an awesome update for you!")
.setSmallIcon(R.drawable.ninja)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ninja, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();

NotificationManager notificationManager = (NotificationManager) getSystemService                         (NOTIFICATION_SERVICE);

// If you want to hide the notification after it was selected, do the code below
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, mNotification);


Output:




























Download Full Source Code: NotificationDemo

Enjoy Coding...!!!

No comments:

Post a Comment