Wednesday, 15 January 2014

Cancel Notification after a given time in Android -



Cancel Notification after a given time in Android -

i trying cancel notification after given time automatically using alarmmanager.

say illustration setting notification through alarmmanger @ 9:00am daily.

i cancel same notification @ 09:30am daily through alarmmanager automatically.

hence, trying the following:

the below code set within notification service

public class servicefornotification extends service { private string ntext; private resources res; @override public void oncreate() { super.oncreate(); ntext = "testing notification"; context context = servicefornotification.this; intent notificationintent = new intent(this, servicetocancelnotification.class); pendingintent contentintent = pendingintent.getservice(this, 0, notificationintent, pendingintent.flag_cancel_current); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); remoteviews bigview = new remoteviews(context.getpackagename(), r.layout.notificationlayoutbig); bigview.settextviewtext(r.id.noticetext, ntext); notification noti = new notification.builder(this) .setcontenttitle(ntext) .setpriority(notification.priority_max) . setcontenttext ("slide downwards expand"). setsmallicon(r.drawable.close) .setlargeicon(bitmapfactory.decoderesource(res, r.drawable.close)) .setticker("test") .setautocancel(true) .setwhen(system.currenttimemillis()) .setcontentintent(contentintent) .setlights(color.magenta, 500, 500).build(); noti.flags |= notification.flag_ongoing_event | notification.priority_max; noti.bigcontentview = bigview; notificationmanager.notify(100, noti); /*18000000*/ intent notificationintent = new intent(this, servicetocancelnotification.class); pendingintent contentintent = pendingintent.getservice(servicefornotification.this, 0, notificationintent, pendingintent.flag_cancel_current); alarmmanager am2 = (alarmmanager) getsystemservice(alarm_service); am2.setexact(alarmmanager.rtc_wakeup, 90000, contentintent); log.e("alarm set","to cancel"); stopself(); } }

i have service called servicetocancelnotification..

which has next code:

public class servicetocancelnotification extends service { @override public void oncreate() { super.oncreate(); log.e("alarm","in cancel"); //cancel notification notificationmanager nm = (notificationmanager)getsystemservice(notification_service); int unique_id = 100; nm.cancel(unique_id); log.e("alarm","notifiy cancel"); stopself(); } }

this servicetocancelnotification not called after alarmmanager triggered. not sure why happening?

can help me prepare out me?

thanks!

// alarm manager , pending intent should global variables, and/or instance should passed cancelalarm alarmmanager am= null; pendingintent pi = null; @override public void onreceive(context context, intent intent) { am=(alarmmanager) context.getsystemservice(context.alarm_service); intent intent = new intent(context, alarmmanagerbroadcastreceiver.class); pi = pendingintent.getbroadcast(context, alarm_id, intent, pendingintent.flag_update_current); } public void cancelalarm() { am.cancel(pi); } public void cancelalarm(alarmmanager am, pendingintent pi) { am.cancel(pi); }

android notifications

No comments:

Post a Comment