簡體   English   中英

如何在Android中以編程方式打開(NotificationListener Service中的StatususBarNotification對象)通知?

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

我在Android中創建了與此代碼類似的NotificationListenerService。 我的應用程序在單獨的窗口中顯示通知。 當用戶單擊我的窗口中的通知時,將打開相應的應用程序。

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
}

我嘗試使用密鑰打開通知。

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

例如,如果單擊電子郵件通知,則該應用程序將啟動電子郵件應用程序。 但是,它不會打開確切的電子郵件活動。 如何從StatusBarNotification對象打開活動。

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

使用鍵打開通知。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM