Wednesday, 27 May 2015

Generate Notification in Android

   Here I am going to explain how to generate notification in android


  public static void generateNotification(Context context, String message)
       {
int icon = R.drawable.image;
long when = System.currentTimeMillis();
NotificationManager notificationManager =(NotificationManager)                               context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, messagewhen );

String title = context.getString(R.string.app_name);

Intent notificationIntent= new Intent(context, Activity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification .setLatestEventInfo(context, title, messageintent);
notification .flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification .defaults |= Notification.DEFAULT_SOUND;

// Vibrate if vibrate is enabled
notification .defaults |= Notification.DEFAULT_VIBRATE;
notificationManager .notify(0, notification );
}