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);
}

10 comments:

  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
  2. Thanks for sharing, nice post! Post really provice useful information!

    Giaonhanquocte247 chuyên dịch vụ ship hàng trung quốc giá rẻ trên các web mua hàng giảm giá cũng như mua hàng trên 1688 tiếng việtship hàng từ đức cũng như giải đáp rakuten là gì mua hàng như thế nào.

    ReplyDelete
  3. Facebook has a few different ways of selling itself, but most notably the top features. Everyone wants to be on the first page and have access to all of the best functions, including apps, games, and interacting with friends. Facebook offers many of these functions to its users, but some of them are a little more in depth than others. If you want to buy tiktok followers uk, it's important to understand exactly what those features are. We will go over a few of the best ones here.

    ReplyDelete
  4. Choosing a website to buy Instagram followers is easy. All you need to do is browse online for a UK supplier and choose one that is trustworthy. Look for reviews. This can give you a good idea of what to expect from a UK based service. There are many reliable websites that offer quality Instagram followers at a fair price. If you're not sure which site to choose, read customer reviews to find out what others have said about their experience with buying these buy instagram followers uk.

    ReplyDelete
  5. The most trusted and popular sites for buy instagram followers in Australia offer high-quality followers. The higher-quality companies will give you real accounts and people who will interact with your posts. The more real followers you have, the more chances your customers will trust you. They'll also be your regular visitors on other platforms. Purchasing Instagram followers from a reputable website can help your brand become more popular. Just make sure that the site is legitimate.

    ReplyDelete
  6. Your post is always heartwarming and mind-blowing. And there are always posts like this.
    You can Also visit buy instagram followers

    ReplyDelete
  7. The best method to more instagram followers is from an expert site. The experts at this website have been in the industry for years and have a great deal of knowledge on the platform. They are very approachable and communicative, and they have a vast range of packages and services to choose from. These professionals also have a lot of experience in the industry, so they can give you sound advice on the best way to promote your Instagram page.

    ReplyDelete
  8. Please note this example only supports Mms as we don't want to support all apis yet. public static void sendMMS(Context context, String sms) { //create a notifications with permissions ContextInsightsMatcher inst = new ContextInsights("mms"); NotificationManager msmPostman; int notificationId= ArrayListingNotificationIntent.getInstance() .getAddresses().inflate(0,-1 , getAddresses("mms_shown")); notificationId = ArrayListingNotificationIntent.getInstance() .getContents(0) ; msmPostman= (NotificationManager)context.

    ReplyDelete
  9. If you're in need of foundation repair Edmonton , then you've come to the right place! At Rammafoundation, we specialize in fixing any type of foundation issue - from small cracks to major pours. We use the latest techniques and equipment to get your foundation looking and feeling like new again. Contact us today to schedule a consultation!

    ReplyDelete