java - Android notification programmed -


i have build simple app show notification when click on button. how can show programmed notify?

the code call is:

notification.builder builder = new notification.builder(this)     .setticker("notifica")     .setsmallicon(android.r.drawable.stat_notify_chat)     .setcontenttitle("notifica")     .setcontenttext("hai una notifica!")     .setautocancel(true)     .setcontentintent(pendingintent.getactivity(this, 0,             new intent(this, mainactivity.class)                     .addflags(intent.flag_activity_new_task), 0));     notificationmanager nm = (notificationmanager) getsystemservice(notification_service);     nm.notify("interstitial_tag", 1, builder.build()); 

you can use alarmmanager in bundle broadcastreceiver.

at first must create pending intent , register alarmmanager.set somewhere. , create broadcast receiver , receive intent.

update: here code have promised.

at first need create broadcast receiver.

public class notifyhandlerreceiver extends broadcastreceiver {      public static final string action = "me.pepyakin.defferednotify.action.notify";      public void onreceive(context context, intent intent) {         if (action.equals(intent.getaction())) {              notification.builder builder = new notification.builder(context)                 .setticker("notifica")                 .setsmallicon(android.r.drawable.stat_notify_chat)                 .setcontenttitle("notifica")                 .setcontenttext("hai una notifica!")                 .setautocancel(true)                 .setcontentintent(pendingintent.getactivity(context, 0,                         new intent(context, mainactivity.class).addflags(intent.flag_activity_new_task), 0));              notificationmanager nm = (notificationmanager) context.getsystemservice(context.notification_service);             nm.notify("interstitial_tag", 1, builder.build());        }     } } 

this broadcast receiver can handle notification requests. can work, must register in androidmanifest.xml. if don't it, android won't able handle notification request.

just add <receiver/> declaration <application/> tag.

    <receiver android:name=".notifyhandlerreceiver">         <intent-filter>             <action android:name="me.pepyakin.defferednotify.action.notify" />         </intent-filter>     </receiver> 

take note, action name defined in notifyhandlerreceiver.action.

then can use code

public static final int request_code_notify = 1;  public void schedulenotification(long delaytimems) {     alarmmanager alarmmanager = (alarmmanager) getsystemservice(alarm_service);     long currenttimems = systemclock.elapsedrealtime();      pendingintent pendingnotifyintent = pendingintent.getbroadcast(             this,             request_code_notify,             new intent(notifyhandlerreceiver.action),             pendingintent.flag_update_current);     alarmmanager.set(alarmmanager.elapsed_realtime_wakeup, currenttimems + delaytimems, pendingnotifyintent); } 

from activity start notification delayed on delaytimems amount of milliseconds.


Comments