The notification of Android development

Source: Internet
Author: User

Android notification is a basic feature in every Android app development, and it can push some messages to the user according to the specified rules, which is a very useful function. This article mainly describes the Android Notification usage of 4 forms, I hope you can help Android developers.

Implementing notifications generally follows several steps:

    • 1. Get the Notification service object Notificationmanager
    • 2. Setting up Notification objects
    • 3. Associated Intent
    • 4. Notification of execution

Notification generally have several uses:, such as

1. Start multiple notifications but show only one

If there are a lot of notifications in the same way, in order to not affect the system notification display too many lists, can be counted stacked, only show one, the code is as follows:

Btn1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                  count++; Get Notification Manager Notificationmanager nm= (Notificationmanager) MainActivity.this.getSystemService from System services (context.no                  Tification_service); Define Notification Notification n=new Notification (r.drawable.ic_launcher, "I am a notification prompt", System.currenttimemillis                  ());                  N.flags=notification.flag_auto_cancel;                  N.number=count;                  The notification message is associated with the Intent Intent it=new Intent (mainactivity.this,second.class);                  It.putextra ("name", "Name:" +count);                  Pendingintent pi=pendingintent.getactivity (mainactivity.this, +, it, pendingintent.flag_cancel_current);                  Specific notification content N.setlatesteventinfo (mainactivity.this, "title", "Start multiple notifications only show one", pi);           Execution notification nm.notify (1, N);   }          }); 

After clicking 4 times, the effect is as follows:

This is because the Requestcode of Nm.notify (1,n) is all the same.

2. Start multiple notifications, and display multiple, but only with the latest one to jump to the activity
Btn2.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                  count++; Get Notification Manager Notificationmanager nm= (Notificationmanager) MainActivity.this.getSystemService from System services (context.no                  Tification_service); Define Notification Notification n=new Notification (r.drawable.ic_launcher, "I am a notification prompt", System.currenttimemillis                  ());                  N.flags=notification.flag_auto_cancel;                  N.number=count;                  The notification message is associated with the Intent Intent it=new Intent (mainactivity.this,second.class);                  It.putextra ("name", "Name:" +count);                  Pendingintent pi=pendingintent.getactivity (mainactivity.this, +, it, pendingintent.flag_cancel_current);                  Specific notification content N.setlatesteventinfo (mainactivity.this, "title", "Start multiple notifications display multiple, but only with the latest notification to jump to the activity", PI); Execution Notification nm.notify (couNT, N); }          });

The results are as follows:

Click 5th, 6 notifications will not jump, only click on the 7th to jump.

And this is caused by the same requestcode of Pendingintent.

3. Start multiple notifications, and display multiple, each notification can jump to the corresponding activity
Btn3.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                  count++; Get Notification Manager Notificationmanager nm= (Notificationmanager) MainActivity.this.getSystemService from System services (context.no                  Tification_service); Define Notification Notification n=new Notification (r.drawable.ic_launcher, "I am a notification prompt", System.currenttimemillis                  ());                  N.flags=notification.flag_auto_cancel;                  N.number=count;                  The notification message is associated with the Intent Intent it=new Intent (mainactivity.this,second.class);                               It.putextra ("name", "Name:" +count);                  Pendingintent pi=pendingintent.getactivity (Mainactivity.this, Count, it, pendingintent.flag_cancel_current);                  Specific notification content N.setlatesteventinfo (mainactivity.this, "title", "Start multiple notifications display multiple, each notification can jump to the corresponding activity", PI);               Execution notification   Nm.notify (count, N); }          });

Because Nm.notify (count,n) code is different each time, the equivalent of each notification associated with a intent, the results and consistent, but each notification can jump to the corresponding activity.

4, Custom layout

The first notification in the message is custom:

The only difference between the custom layout and the normal way is that a remoteview is loaded, and the fragment is as follows:

Remoteviews rv=new remoteviews (MainActivity.this.getPackageName (), r.layout.notify);                  N.CONTENTVIEW=RV;                  The notification message is associated with the Intent                  Intent it=new Intent (mainactivity.this,second.class);                  It.putextra ("name", "Name:" +count);                               Pendingintent pi=pendingintent.getactivity (Mainactivity.this, Count, it, 0);                  N.contentintent=pi;

This custom notification I was going to implement define a button, click the button and then cancel the notification. My idea is as follows:

Since RemoteView does not have a findviewbyid-like approach, I can't find a control on a newly loaded layout file, and I can't add a button event for it.

However, in the Association of Intent, Pendingintent provides a number of methods, mainly:

As you can see, you can associate three components.

Moreover, although RemoteView is not Findviewbyid, he has setonclickfillinintent and setonclickpendingintent two methods

I set up a broadcast first.

Register broadcast          Broadcastreceiver b=new broadcastreceiver () {              @Override public              void OnReceive (context context, Intent Intent) {                  Toast.maketext (context, "button clicked", "N"). Show ();              }          ;          Intentfilter filter=new intentfilter ();          Filter.addaction ("ONCLICK");          This.registerreceiver (b, filter);

Then continue to set the control's events after nm.notify

Execution Notification                  nm.notify (count, n);                  Set the control                  rv.settextviewtext (R.ID.TV2, "I am a custom layout");                  Intent clickintent=new Intent ("ONCLICK");                  Pendingintent Clickpending=pendingintent.getbroadcast (Mainactivity.this, Count, clickintent, 0);                  Rv.setonclickpendingintent (R.id.cancel, clickpending);

The results are not executed, the broadcast did not receive, I feel that the code is not a problem, look at the next setonclickpendingintent, unexpectedly said to Lisetview and other control events do not work, and see the Setonclickfillinintent, This seemingly useful, the system also recommended another similar method, because level is not enough, can not be used, I did not verify, it should be possible, or so many application good-looking notice is what I do ah, first write down, after the need for a good study.

Reference code: http://download.csdn.net/detail/kkkkkxiaofei/6723903

The notification of Android development

Related Article

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.