简体   繁体   English

如何在Android中以编程方式打开(NotificationListener Service中的StatususBarNotification对象)通知?

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

I have created a NotificationListenerService in Android similar to this code . 我在Android中创建了与此代码类似的NotificationListenerService。 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. 如何从StatusBarNotification对象打开活动。

Replace YOURACTIVITY with the activity you want to open on click of notification 将“ YOURACTIVITY”替换为您要在单击通知时打开的活动

    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());

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) {
                }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoSuchMethodError:android.service.notification.StatusBarNotification.getKey - java.lang.NoSuchMethodError: android.service.notification.StatusBarNotification.getKey 错误通知:无法展开RemoteViews:StatusBarNotification。在Android Nougat上 - Bad Notification: Couldn't expand RemoteViews for: StatusBarNotification. on Android Nougat 如何从后台服务的全屏意图通知 Android 打开活动 - How to open activity from background service's full screen intent notification Android 如何从 android 的通知中停止前台服务? - How to stop a foreground service from the notification in android? 如何在 Android 中以编程方式注册服务? - How to register a Service programmatically in Android? 如何从通知中打开片段? - How to open fragment from notification? 当从 Android 的锁定屏幕单击通知操作时,如何以编程方式解锁屏幕? - How can I unlock screen programmatically when the notification action is clicked from lock screen in Android? 如何在Android中单击通知打开相同的活动? - How to open same activity on click of notification in android? 如何打开android快速通知设置 - How to open the android quick notification setting 如何在 Android Studio 中的后台应用程序时从 Android 通知中打开 URI - How to open URI from Android notification when the app on background in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM