java - How to open a (StatusBarNotification object from NotificationListener Service )notification in Android programmatically? -


i have created notificationlistenerservice in android similar code. app displays notifications in separate window. when user clicks notification in window, corresponding app opened.

public void onnotificationposted(statusbarnotification sbn) {          bundle extras = sbn.getnotification().extras;         string title = getstringfrombundle(extras, "android.title");         string subtext = getstringfrombundle(extras, "android.subtext");         string text = getstringfrombundle(extras, "android.text");         string bigtext = getstringfrombundle(extras, "android.bigtext");         string array[] = { title, subtext, text, bigtext };         int progress = extras.getint("android.progress", 0);         int progressmax = extras.getint("android.progressmax", 0);         int int_array[] = { progress, progressmax };         notification_added(sbn, array, int_array, bitmap); //adds notification in list } 

i try open notification using key.

public void opennotification(string key) {         string keys[] = { key };         statusbarnotification sbns[] = getactivenotifications(keys);         (statusbarnotification sbn : sbns) {                 try {                         if (sbn == null) {                                 log.i(tag, "sbn null");                                 continue;                         }                         /*                            notification n = sbn.getnotification();                            if (n.contentintent != null) {                            pendingintent pi = n.contentintent;                            if (pi != null) {                            pi.send(this, 0, null);                            }                            }                          */                         cancelnotification(key);                         intent intent = getpackagemanager().getlaunchintentforpackage(                                         sbn.getpackagename());                         if (intent != null) {                                 log.i(tag, "launching intent " + intent + " package name: "                                                 + sbn.getpackagename());                         }                 } catch (exception e) {                 }         } } 

for example, if email notification clicked, app launches email app. but, not opens exact email activity. how open activity statusbarnotification object.

replace youractivity activity want open on click of notification

    intent intent = new intent(getbasecontext(), youractivity.class);     pendingintent contentintent = pendingintent.getactivity(getbasecontext(), 0, intent, pendingintent.flag_update_current);      notificationcompat.builder b = new notificationcompat.builder(getbasecontext());      b.setautocancel(true)             .setdefaults(notification.default_all)             .setwhen(system.currenttimemillis())             .setsmallicon(r.drawable.ic_launcher)             .setticker("ticker")              .setcontenttitle("title")              .setcontenttext("message")              .setdefaults(notification.default_lights | notification.default_sound)             .setcontentintent(contentintent)             .setcontentinfo("info");       random r = new random();     int randomno = r.nextint(100000000 + 1);      notificationmanager notificationmanager = (notificationmanager) getbasecontext().getsystemservice(context.notification_service);     notificationmanager.notify(randomno, b.build()); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -