I haven’t written the usage of notice bar all the time. Today I just see it here, so I summarize it here.

1. Related methods of Notification.Builder

  • SetWhen: Sets the push time in the format of hour: minute. The notification time is displayed on the right side of the notification bar.
  • SetShowWhen: Sets whether to display the push time.
  • SetUsesShronometer: Sets whether to display timers. When this parameter is true, the push time is not displayed and the time interval from the notification to the current is displayed dynamically in the format of “minute: second”.
  • SetSmallIcon: Sets the icon (small icon) in the status bar.
  • SetTicker: Sets the prompt text in the status bar.
  • SetLargeIcon: Sets the icon (large icon) in the notification bar.
  • SetContentTitle: Sets the title text in the notification bar.
  • SetContentText: Sets the content text in the notification bar.
  • SetSubText: Sets the caption text in the notification bar, below the content text. If you set this method, the setProgress setting becomes invalid.
  • SetProgress: Sets the progress bar and the current progress. The progress bar is located between the title text and the content text.
  • SetNumber: Sets the number in the lower right corner of the notification bar. It can be used in conjunction with setProgress to indicate the current progress value.
  • SetContentInfo: Sets the text at the bottom right of the notification bar. If this method is called, the setNumber setting is invalid.
  • PendingIntent: Set the intent to be delayed. Click on the notification to trigger the intent. Typically, the PendingIntent getActivity method is called to retrieve the deferred intent object. GetActivity represents a click to jump to the page.
  • PendingIntent: setDeleteIntent: Sets the intent to delay the deletion.
  • SetAutoCancel: Sets whether the notification is cleared automatically. If it is true, the notification will disappear automatically after clicking the notification; if it is flase, the notification will not disappear after clicking the notification.
  • SetContent: Sets a custom notification view RemoteViews to replace the Builder’s default view template.
  • Build: the construction method. After all the above parameters are set, this method is called to return a Notification object.

2. Precautions for using the above methods

  • The setSmallIcon method must be called otherwise no notification message will be displayed.
  • SetWhen and setUsesChronometer can only be called at the same time, that is, the push time and counter cannot be displayed at the same time, because they are both located on the right side of the notification bar.
  • SetSubText and setProgress can only be called at the same time because the caption and progress bar are located below the header text.
  • SetNumber and setContentInfo can only be called at the same time because both the count and the hint are located at the bottom right of the notification bar.

3. Related methods of NotificationManager

  • Notify: Push a specified message to the notification bar
  • Cancel: Cancels the specified message. When this method is called, the specified message in the notification bar disappears
  • CancelAll: Cancels all messages (to be continued)

4. Common message example

private void sendSimpleNotify(String title,String message){ count++; // get the NotificationManager from the system service NotificationManager notifyMgr = (NotificationManager) getSystemService(context.notification_service); CreateNotifyChannel (notifyMgr,this,"channel_id") is executed only after 8.0; / / create a jump to the intention of the activity page Intent clickIntent = new Intent (this, MessageActivity. Class); clickIntent.putExtra("flag",count); / / create a delay for page jump intention PendingIntent contentIntent = PendingIntent. GetActivity (this, count, clickIntent ,PendingIntent.FLAG_UPDATE_CURRENT); // Create a Notification message Builder.Builder Builder = new Notification.Builder(this); If (android. OS. Build. VERSION. SDK_INT > = android. OS. Build. VERSION_CODES. O) {/ / Android8.0 start must inform distribution corresponding to each channel builder = new Notification.Builder(this,"channel_id"); } Builder.setContentIntent (contentIntent)// Sets the click intent of the content.setautoCancel (true)// Sets whether to allow automatic clearing .setSmallicon (r.map.ic_launcher)// Sets the small icon in the status bar. SetTicker (" Prompt message coming ")// Sets the prompt text in the status bar .setwhen (system.currentTimemillis ())// Set the push time to "hour: Minutes ". SetLargeIcon (BitmapFactory decodeResource (getResources (), R.m ipmap. Ic_launcher)) / / set the notification bar inside the large ICONS .setContentTitle(title)// Set the title text inside the notification bar. SetContentText (message); / / / / set the notification bar inside the content of the text according to the constructor to create a notification object if (android. OS. Build. VERSION. SDK_INT. > = android OS. Build. VERSION_CODES. JELLY_BEAN) { Notification notify = builder.build(); Notifymgr. notify(count,notify); }} /** * Create notification channel, Android8.0 must assign channel to each notification * @param notifyMgr * @param CTX * @param channelId */ private void createNotifyChannel(NotificationManager notifyMgr,Context ctx,String channelId){ if (android.os.Build.VERSION.SDK_INT >= Android.os.build.version_codes.o) {// create a default importance NotificationChannel NotificationChannel channel = new NotificationChannel(channelId,"Channel",NotificationManager.IMPORTANCE_DEFAULT); channel.setSound(null,null); channel.setShowBadge(true); channel.canBypassDnd(); // Can bypass do not Disturb mode channel.enableLights(true); / / flash channel. SetLockscreenVisibility (Notification. VISIBILITY_SECRET); // Lock screen to display notification channel.setlightColor (color.red); Channel.canshowbadge (); EnableVibration (true); // Can vibrate channel.getGroup(); / / get notification channel. The channel group setVibrationPattern (new long [] {100100200}); // Vibratemode channel.shouldshowlights (); / / will flash notifyMgr createNotificationChannel (channel); }}Copy the code

Note here that we use a count variable to differentiate each message, so count must change or the reply will overwrite the previous one.

5. Timing message example

private void sendSimpleNotify(String title,String message){ count++; // get the NotificationManager from the system service NotificationManager notifyMgr = (NotificationManager) getSystemService(context.notification_service); CreateNotifyChannel (notifyMgr,this,"channel_id") is executed only after 8.0; / / create a jump to the intention of the activity page Intent clickIntent = new Intent (this, MessageActivity. Class); clickIntent.putExtra("flag",count); / / create a delay for page jump intention PendingIntent contentIntent = PendingIntent. GetActivity (this, count, clickIntent ,PendingIntent.FLAG_UPDATE_CURRENT); // Create a Notification message Builder.Builder Builder = new Notification.Builder(this); If (build.version.sdk_int >= build.version_codes.o) {//Android8.0 must allocate the corresponding channel builder = new for each notification Notification.Builder(this,"channel_id"); } if (build.version.sdk_int >= build.version_codes.jelly_bean) {builder.setContentIntent(contentIntent)// Sets the click intent for the content SetUsesChronometer (true)// sets whether to display the counter. SetProgress (100,60,false)// sets the progress bar with the current progress .setNumber(99)// Set the number at the bottom right of the notification bar. SetSmallIcon (r.map.ic_launcher)// Set the small icon in the status bar. SetTicker (" Prompt message coming ")// Set the prompt text in the status bar SetLargeIcon (BitmapFactory. DecodeResource (getResources (), R.m. Ipmap ic_launcher)) / / set the large icon in notification bar .setContentTitle(title)// Set the title text inside the notification bar. SetContentText (message); Notification notify = builder.build(); Notification notify = builder.build(); Notifymgr. notify(count,notify); }} /** * Create notification channel, Android8.0 must assign channel to each notification * @param notifyMgr * @param CTX * @param channelId */ private void createNotifyChannel(NotificationManager notifyMgr,Context ctx,String channelId){ if (android.os.Build.VERSION.SDK_INT >= Android.os.build.version_codes.o) {// create a default importance NotificationChannel NotificationChannel channel = new NotificationChannel(channelId,"Channel",NotificationManager.IMPORTANCE_DEFAULT); channel.setSound(null,null); channel.setShowBadge(true); channel.canBypassDnd(); // Can bypass do not Disturb mode channel.enableLights(true); / / flash channel. SetLockscreenVisibility (Notification. VISIBILITY_SECRET); // Lock screen to display notification channel.setlightColor (color.red); Channel.canshowbadge (); EnableVibration (true); // Can vibrate channel.getGroup(); / / get notification channel. The channel group setVibrationPattern (new long [] {100100200}); // Vibratemode channel.shouldshowlights (); / / will flash notifyMgr createNotificationChannel (channel); }}Copy the code

That’s all for today. If you have any questions, let me know in the comments section.