smartkid
smartkid

Reputation: 1549

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

I have created a NotificationListenerService in Android similar to this code. My app displays the notifications in a separate window. When a user clicks the notification in my window, corresponding app is 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 the notification in a list
}

I try to open the notification using the key.

public void OpenNotification(String key) {
        String keys[] = { key };
        StatusBarNotification sbns[] = getActiveNotifications(keys);
        for (StatusBarNotification sbn : sbns) {
                try {
                        if (sbn == null) {
                                Log.i(TAG, "sbn is 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 is clicked, the app launches the email app. But, it does not opens the exact email activity. How to open the activity from StatusBarNotification object.

Upvotes: 1

Views: 2117

Answers (1)

smartkid
smartkid

Reputation: 1549

To open a notification using a key.

public void OpenNotification(String key) {
        String keys[] = { key };
        StatusBarNotification sbns[] = getActiveNotifications(keys);
        for (StatusBarNotification sbn : sbns) {
                try {
                        if (sbn == null) {
                                Log.i(TAG, "sbn is null");
                                continue;
                        }
                        Notification n = sbn.getNotification();
                        if (n.contentIntent != null) {
                                PendingIntent pi = n.contentIntent;
                                if (pi != null) {
                                        pi.send();
                                }
                        }
                } catch (Exception e) {
                }
        }
}

Upvotes: 1

Related Questions