The main components of the Notification are the ticker, title, content, 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); }
Hello Sherif,
ReplyDeleteThank 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!
Great Article. Thank you for sharing! Really an awesome post for every one.
DeleteAvoiding Data Corruption in Drop Computing Mobile Networks Project For CSE
EdgeCare Leveraging Edge Computing for Collaborative Data Management in Mobile Healthcare Systems Project For CSE
Energy Efficient Mobile Service Computing With Differential Spintronic C ElementsA Logic in Memory Asynchronous Computing Paradigm Project For CSE
Formal Analysis of Language Based Android Security Using Theorem Proving Approach Project For CSE
Group Wise Itinerary Planning in Temporary Mobile Social Network Project For CSE
Image Steganography Based on Foreground Object Generation by Generative Adversarial Networks in Mobile Edge Computing With Internet of Things Project For CSE
Interest Driven Outdoor Advertising Display Location Selection Using Mobile Phone Data Project For CSE
i VALS Visual Attention Localization for Mobile Service Computing Project For CSE
Thanks for sharing, nice post! Post really provice useful information!
ReplyDeleteGiaonhanquocte247 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ệt và ship hàng từ đức cũng như giải đáp rakuten là gì mua hàng như thế nào.
Engineering mathematics 1
ReplyDeleteVTU 2nd SEM syllabus
Basic Electronics Engineering Tuition Classes in Vijayanagar, Bangalore
Basic electrical engineering Tuition classes for B.tech students
Best Java Training in Bangalore
Java Training institute in Bangalore
Best Java Training institute in Bangalore
Java Training institutes in Bangalore
Best Java Training institutes in Bangalore
ReplyDeleteتنظيف منازل بالدمام تنظيف منازل بالدمام
تنظيف منازل بالاحساء تنظيف منازل بالاحساء
تنظيف منازل بمكة تنظيف منازل بمكة
تنظيف منازل بجدة تنظيف منازل بجدة
تنظيف منازل بالمدينة المنورة تنظيف منازل بالمدينة المنورة
Internet Business Ideas for 2020
ReplyDeleteBenefits Of Micro Switch and More About the China Micro Switch Company
ReplyDeleteThanks for Sharing a Very Informative Post & I read Your Article & I must say that is very helpful post for us.
ReplyDeleteData Science
Selenium
ETL Testing
AWS
Python Online Classes