Android Notification bar notification Click, Cancel, clear response event

Source: Internet
Author: User

Response events that detect three states of the Android notification bar primarily

This time in the implementation of push demand, to use the Android notification bar notification Click to enter the Message page, because to achieve a save push user name function, I clicked on after the function, but the test found that I click Delete or swipe to clear after this function is not executed, That's why you're aware of the events to handle delete and slide cleanup:

First implement a broadcastreceiver

 Public classNotificationbroadcastreceiverextendsBroadcastreceiver { Public Static FinalString type = "type";//This type is for notification update information, this does not understand the friend can go to search, a lot of@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); intType = Intent.getintextra (type,-1); if(Type! =-1) {Notificationmanager Notificationmanager=(Notificationmanager) Context.getsystemservice (Context.notification_service);        Notificationmanager.cancel (type); }        if(Action.equals ("notification_clicked")) {            //Handling Click events        }                if(Action.equals ("notification_cancelled")) {            //handling swipe cleanup and click Delete Events        }    }}

and the registration Notification is this:

Intent Intentclick =NewIntent ( This, Notificationbroadcastreceiver.class); Intentclick.setaction ("Notification_clicked"); Intentclick.putextra (Notificationbroadcastreceiver.type, TYPE); Pendingintent Pendingintentclick= Pendingintent.getbroadcast ( This, 0, Intentclick, pendingintent.flag_one_shot); Intent intentcancel=NewIntent ( This, Notificationbroadcastreceiver.class); Intentcancel.setaction ("Notification_cancelled"); Intentcancel.putextra (Notificationbroadcastreceiver.type, TYPE); Pendingintent Pendingintentcancel= Pendingintent.getbroadcast ( This, 0, Intentcancel, pendingintent.flag_one_shot); Uri Defaultsounduri=Ringtonemanager.getdefaulturi (ringtonemanager.type_notification); Notificationcompat.builder Notificationbuilder=NewNotificationcompat.builder ( This). Setsmallicon (R.drawable.ic_launcher). Setcontenttitle (GetString (R.string.setting_gcm_title)). Setcontenttext (Message). Setsound (Defaultsounduri). Setcontentintent (Pendingintentclick). Setdeletein Tent (pendingintentcancel); Notificationmanager=(Notificationmanager) Getsystemservice (Context.notification_service); Notificationmanager.notify (Type/*ID of notification*/, Notificationbuilder.build ());//This is the type, the same update, the different add 

Of course, don't forget to register in Androidmanifest.xml .

<receiverAndroid:name= ". Gcm. Notificationbroadcastreceiver ">    <Intent-filter>        <ActionAndroid:name= "notification_cancelled"/>        <ActionAndroid:name= "notification_clicked"/>    </Intent-filter></receiver>

This is the initial implementation of the Android notification bar notification Click, Cancel, clear response events

Android Notification bar notification Click, Cancel, clear response event

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.