Idea Map
Mainactivity.class
public class Mainactivity extends Appcompatactivity implements view.onclicklistener{Notificationmanager Manager; int notification_id = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (r.id.send_message). Setonclicklistener (this); Findviewbyid (r.id.cancel_message). Setonclicklistener (this); Manager = (Notificationmanager) getsystemservice (Context.notification_service); Get notification Control class} @Override public void OnClick (View v) {switch (V.getid ()) {r.id.send_message: {Send_message_method (); Break } Case r.id.cancel_message:{Cancel_message_method (); Break }}}//Build send notification method private void Cancel_message_method () {manager.cancel (notification_id); }//Cancel notification bar private void Send_message_method (){Intent itent = new Intent (this,mainactivity.class); Pendingintent pitent = pendingintent.getactivity (this,0,itent,0); Build builder builder for Notification notification.builder builder = new Notification.builder (this); Builder.setsmallicon (R.mipmap.ic_launcher); Set icon Builder.setticker ("Hello, world! "); Set the phone status bar prompt information Builder.setwhen (System.currenttimemillis ()); Set the time Builder.setcontenttitle ("title of the World Game"); Set the title Builder.setcontenttext (Goose Goose Goose, Song to Heaven!) "); Set notification content builder.setcontentintent (pitent); Set the post-click intent//Builder.setdefaults (notification.default_lights);//Set Prompt indicator//builder.setdefaults (NOTIFICATION.D Efault_sound)///Set Prompt sound//builder.setdefaults (notification.default_vibrate);//Set Prompt vibration Builder.setdefaults (Noti Fication. Default_all); All settings are Notification Notification = Builder.build ()//4.1 above manager.notify (notification_id,notification); }}
Note the permissions when setting defaults. Vibration, sound permission.
<uses-permission android:name= "Android. PERMISSION. Flashlight "/>
<uses-permission android:name= "Android.permission.VIBRATE"/>
Basic understanding of notification bar construction and cancellation