Use of Android Notification

Source: Internet
Author: User

has been studying Android for a while and has been working on one side of the app. It has encountered the issue of notifying the program that it is resident in the Notification column and cannot be clear (just like Android QQ). After research and realization of its function, now the use of Notification is summarized as follows:

The use of Notification requires importing 3 classes

123 importandroid.app.PendingIntent;import android.app.NotificationManager;importandroid.app.Notification;

Code Examples and Descriptions

123456789101112131415161718 NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);               Notification n = newNotification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());             n.flags = Notification.FLAG_AUTO_CANCEL;                Intent i = new Intent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);           //PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(        arg0.getContext(),         R.string.app_name,         i,         PendingIntent.FLAG_UPDATE_CURRENT);                n.setLatestEventInfo(        arg0.getContext(),        "Hello,there!",         "Hello,there,I‘m john.",         contentIntent);nm.notify(R.string.app_name, n);

Each piece of code is analyzed in turn:

1 NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Creates a notificationmanager that creates a NM object responsible for "emitting" and "canceling" Notification.

12 Notification n = newNotification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());             n.flags = Notification.FLAG_ONGOING_EVENT; 

Create Notification, with the parameters: Icon's resource ID, scrolling information displayed on the status bar, time. The N object that is created is used to describe the information that appears in the system notification bar, and we will then see the intent that is set on the N object to click on this notification.

1 n.flags = Notification.FLAG_AUTO_CANCEL;

Set N.flags to Notification.flag_auto_cancel, which indicates that the notification can be cleared when the user clicks clear.

12 Intent i = newIntent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

Create a intent that intent make this intent when the user clicks the notification

Note that if you want to start an ACTIVITY with the Intent, be sure to set the Intent.flag_activity_new_task tag.

Intent.flag_activity_clear_top: If there is activity to start in the current task, turn off all activity before the acitivity, and to avoid creating instances of the activity by placing this activity before

Intent.flag_activity_new_task: The system checks whether any currently created task has a task for the activity to start, and if so, creates an activity on that task, If not, a new task with the Activity property is created, and an activity on the new task. See also "  (reprint) Android under Affinities and task "

123456 < Code class= "Java comments" >//pendingintent pendingintent contentintent = Pendingintent.getactivity (          arg0.getcontext (),           r.string.app_name,          i, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; pendingintent.flag_update_current);

Pendingintent for Intent packaging, here is a description of the start intent, Pendingintent.getactivity returns the Pendingintent said, The intent in this pendingintent instance is the intent used to start the Activity. The parameters of the pendingintent.getactivity are: Context, the sender's request code (can fill 0), for the system to send the intent, flag bit.

Where pendingintent.flag_update_current indicates that if the pendingintent of the description already exists, The extra data for the existing pendingintent is changed to the extra data for the new pendingintent.

Here is a brief talk about the difference between Intent and pendingintent:

Intent: The intention, that is to tell the system what I want to do, and then the system according to this Intent do the corresponding thing. such as startactivity is equivalent to sending a message, while intent is the content of the message.

Pendingintent: Packaging Intent,intent is our intention to use startactivity, StartService, or sendbroadcast to start a work directly. At some point, we can not call startactivity, startservide or sendbroadcast directly, but send intent when a program or system reaches a certain condition. As notification here, when the user clicks Notification, the system issues an activity Intent. So if we don't have a way to tell the system, the system doesn't know whether to use StartActivity, StartService or Sendbroadcast to start intent (and of course other "descriptions"), So here we need pendingintent.

12345 n.setLatestEventInfo(        arg0.getContext(),        "Hello,there!",         "Hello,there,I‘m john.",         contentIntent);

Set the information displayed in the Notification drop-down box, with the following parameters: Context, title, content, Pendingintent.

1 nm.notify(R.string.app_name, n);

Start notification with the following parameters: Identify the ID value of the notification in your program (used to distinguish between different notifycation in the same program, if there is only one notification in the program then you can fill anything you want, However the type must be int), the notification to be notified.

How to make your own notification like Android QQ can appear under the "Running" column

Actually very simple, just set notification.flags = Notification.flag_ongoing_event;

How to change the layout of Notification under the "Running" column

Create Remoteviews and assign it to Notification.contentview, and then assign Pendingintent to Notification.contentintent, such as:

12345678910111213 PendingIntent contentIntent = PendingIntent.getActivity(    arg0.getContext(),     R.string.app_name,    i,     PendingIntent.FLAG_UPDATE_CURRENT);            RemoteViews rv = newRemoteViews(Main.this.getPackageName(), R.layout.notification_view);rv.setImageViewResource(R.id.image, R.drawable.chat);rv.setTextViewText(R.id.text, "Hello,there,I‘m john.");n.contentView = rv;n.contentIntent = contentIntent;nm.notify(R.string.app_name, n);

Note that if Contentview is used, then do not use Notification.setlatesteventinfo. If the setlatesteventinfo is assigned to the Notification.contentview code, then the Contentview effect will be overwritten, showing the effect of setlatesteventinfo; Setlatesteventinfo before the Notification.contentview code, then the Notification.contentview effect is displayed, That is, regardless of the custom effect you want to setlatesteventinfo or Contentview, make sure there is always only one set of code, because when the last sentence is bound, The previous setup of Contentview or Setlatesteventinfo code is completely unnecessary.

Use of Android Notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.