1. Customize a notification layout with buttons: layout_notification;
2. Create notification:
Remoteviews views = new Remoteviews (Getpackagename (), r.layout.layout_nitification); Custom layout views
button click event:
Pendingintent homeintent = Pengdingintent.getbroadcast (this,1,new Intent ("action"), Pengdingintent.flag_update_ Current);
Views.setonclickpendingintent (r.id.btn,homeintent); Click on the ID, click event
To create a notification:
Notificationcompat.builder Mbuilder = new Notificationcompat.builder (getapplicationcontext);
Mbuilder.setcontent (views)//Set layout
. Setongoing (TRUE)//Set whether resident, true for resident
. Setsmallicon (R.mipmap.ic_laucher)//Set Small icons
. Setticker ("Notifications come Up")//Setup Hints
. SetPriority (Notification.priority_max)//Set Priority
. Setwhen (System.currenttimemillis ())//Set display time
. Setcontentintent (Pendingintent.getbroadcast (this,2,new Intent ("Action.view"), pengingintent.flag_update_current ); Set View Click events
Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Notification_service);
Mnotificationmanager.notify (100,mbuilder.build ())//Display notification: Current Notificationid, current notification
3. Create Notificationbroadcast to receive notifications:
public class Notificationbroadcast extends Broadcastreceiver {
@Override
public void OnReceive (Context Context, Intent Intent) {
String action = intent.getaction (); Action
Collapsestatusbar (context); Close the notification bar
Intent i = new Intent (context,mainactivity.class);
I.addflags (Intent.flag_activity_new_task); Must be added to avoid repeatedly opening
if (Action.equals ("Com.xczl.smart.notification.home")) {
I.putextra ("flag", "home"); Value
context.startactivity (i);
}
}
public void Collapsestatusbar (context context) {
try {
Object Statusbarmanager = Context.getsystemservice ("StatusBar");
Method collapse;
if (Build.VERSION.SDK_INT <= 16) {
Collapse = Statusbarmanager.getclass (). GetMethod ("collapse");
} else {
Collapse = Statusbarmanager.getclass (). GetMethod ("Collapsepanels");
}
Collapse.invoke (Statusbarmanager);
} catch (Exception localexception) {
Localexception.printstacktrace ();
}
}
4. Mainactivity Receive Message:
Overwrite Onnewintent ():
@Override
protected void Onnewintent (Intent Intent) {
String extra = Intent.getstringextra ("flag");
if (! Textutils.isempty (extra)) {
if (Extra.equals ("Home")) {
Specific actions to receive a value
}
}
}
5. Configuration Checklist:
Mainactivity: Set Lanchmode to Singletask
Registration broadcast:
<receiver
Android:name= "Com.xczl.smart.broadcast.NotificationBroadcast"
Android:enabled= "true" >
<intent-filter>
<action android:name= "Action"/>
<action android:name= "Action.view"/>
</intent-filter>
</receiver>
To configure notification bar scaling permissions:
<uses-permission android:name= "Android.permission.EXPAND_STATUS_BAR"/>
Android custom notification with buttons and click events and Scaling notification bar