How to send status Notification android

Here is a small function you can use to send a notification in android using the new Api which recommends using Notification.Builder. It still works on older 2.2+ devices.

The main components of the Notification are the tickertitlecontent, and intent:



ticker : this is the text that shows on the top of the screen on the notification bar once the notification is received -i.e. "Someone just pinged you" or "John Smith likes your event"
title
 : this is the title of the notification that appears on the top of the notification
content
 : this is the text of the notification that appears usually below the title
intent
: this is the intent that describes the behavior when the user presses this notification, namely the Activity that starts when this notification is presed.

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void sendNotification(String message) {
 //we need to prepare the Notification object
 Notification notification; 

 //this is the title of the notification
 CharSequence contentTitle = getString(R.string.app_name);
 // this is the text that flows by in the status bar when the notification first activates.
 CharSequence ticker = message;
 //this is the content of the notification
 CharSequence contentText = message;
 
 //we need an Identifier for this notification
 //in this case I am trying to get a unique one because i dont want new notifications to replace old ones
 int id = Integer.valueOf(String.valueOf(System.currentTimeMillis()).substring(5));
 
 //let us prepare the intent
 //my intent will start the activity ActivityNotification
 Intent notificationIntent = new Intent(this, ActivityNotification.class);
 //put any extra needed in my activity
 notificationIntent.putExtra(S.extra.notification, message);
 //my design requires that only one ActivityNotification to be open so I set the FLAG_ACTIVITY_SINGLE_TOP flag
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 //this is the pending intent (when user presses the notification)
 PendingIntent contentIntent = PendingIntent.getActivity(this, id,
   notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 
 //use the new API if our phone's sdk is jelly bean or more
 if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
  Notification.Builder builder = 
    new Notification.Builder(getApplicationContext())
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setOnlyAlertOnce(true)
    .setSmallIcon(R.drawable.ic_launcher)
    .setTicker(ticker)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setWhen(System.currentTimeMillis())
    .setContentIntent(contentIntent);
  notification = builder.build();
 }
 //use the old API otherwise
 else {
  notification = new Notification(R.drawable.ic_launcher, ticker, System.currentTimeMillis());
  notification.defaults = Notification.DEFAULT_ALL;
  notification.flags = Notification.FLAG_AUTO_CANCEL;
  Context context = getApplicationContext();
  notification.setLatestEventInfo(context, contentTitle, contentText,
    contentIntent);
 }
 //get the Notification manager
 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 //send the Notification
 mNotificationManager.notify(id, notification);
}

1 comment:

  1. Hello Sherif,
    Thank you for posting such an interesting code! However, i've been trying a lot to make it work on APIs less than 11 , but nothing's working. I tried NotificationCompat.Builder and Notification.

    1)When you say "use" the old API what do you mean?
    2)Does the last method still work? Or should I use NotificationCompat.Builder instead?

    I appreciate you taking the time to answer my question. Much love bro. You're amazing!

    ReplyDelete