Look at the effect
The layout is so simple, I'm not going to put it on it. I'll show you the core code. int Notificationid = 1;
//set the intent after clicking NotificationsIntent Intent =NewIntent ( This, Notificationview.class); Intent.putextra ("Notificationid", Notificationid); //plug-in pendingintent parameter explanation: 1. Context 2. Request code (Request code for Intent) 3. Intent (to initiate the intent of the target activity) 4. Flag (flag used when activity starts )Pendingintent pendingintent = pendingintent.getactivity ( This, 0, intent,pendingintent.flag_cancel_current); //get notification Management services for your systemNotificationmanager Manager =(Notificationmanager) Getsystemservice (Notification_service); //It is now officially recommended to build notification in the form of builder, which is the new way of being obsolete. Notification.builder Builder =NewNotification.builder ( This); //The information that appears at the top when you receive a notificationBuilder.setsmallicon (R.mipmap.ic_launcher); Builder.setticker ("Reminder:meeting starts in 5 minutes"); Builder.setwhen (System.currenttimemillis ()); //Drop -down top See the detailed notification information icon CommonBuilder.setcontenttitle ("System Alarm");//Set TitleBuilder.setcontenttext ("meeting with customer at 3pm..");//Set Content//Setting the intent of the notification to be clicked is to open an activity//As for why use Pendingintent, because the Pendingintent object can help you perform an action at some later time on behalf of the application, "regardless of whether the application is running"builder.setcontentintent (pendingintent); Builder.setdefaults (Notification.default_sound); //Set the default sound and vibration//Of course you can also customize the set of vibrations//builder.setvibrate (New long[]{100,250,100,500});//Set VibrationBuilder.setautocancel (true);//set the click to disappear automatically, that is, to cancel the display true: click to disappear; false: not disappear after clicking; false by defaultNotification Notif = Builder.build ();//Build here Note build method minimum support MinsdkversionManager.notify (notificationid,notif);//Notification Management Service sends notifications you build you'll get a notification at this point
Note the inside of the Builder.setautocancel (true); If you do not want the program to automatically help you click and close, but your own code in another location to close, then you can not write this code, you can set to False, and then close the code can write
// access to services Notificationmanager nm = (Notificationmanager) Getsystemservice (notification_service); Nm.cancel (Getintent (). Getintextra (// turn off notifications showing Notificationid to 1
Remember to close the ID that matches your notification. Study hard, day after day, so much more.
Study record, if wrong please point out, thank you!
Learn Android from scratch--Notification notifications