2017-07-21 43 views
0

我有一個應用程序,它讀取設備中的所有通知,然後將該通知存儲在Activity包含ListView之後,然後從狀態欄中刪除該通知。爲此,我使用了「NotificationListenerService」。如何閱讀和打開Android中的所有通知?

當我點擊包含通知的ListView時,我想在其目標應用程序中打開該通知。我怎麼做?

碼我用:

public void onNotificationPosted(StatusBarNotification sbn) { 
    Log.i("Msg", "Notification Posted"); 
    String pack = sbn.getPackageName(); 
    String ticker = ""; 
    if (sbn.getNotification().tickerText != null) { 
     ticker = sbn.getNotification().tickerText.toString(); 
    } 
    Bundle extras = sbn.getNotification().extras; 
    String title = extras.getString("android.title"); 
    String text = null; 
    if (extras.getCharSequence("android.text") != null) { 
     text = extras.getCharSequence("android.text").toString(); 
    } 
    Bitmap id = sbn.getNotification().largeIcon; 
    ; 
    if (id != null) { 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     id.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byteArray = stream.toByteArray(); 
    } 

    String jsonData = sharedPreference.getAppsArrayListData(); 
    Type type = new TypeToken<ArrayList<WhiteListModel>>() { 
    }.getType(); 
    whiteListModels = gson.fromJson(jsonData, type); 

    if (whiteListModels != null && whiteListModels.size() > 0) 
    //loop to insert notification data into db. 
    { 
     for (int i = 0; i < whiteListModels.size(); i++) { 
      model = whiteListModels.get(i); 
      if (pack.equals(model.getPackName())) { 
       dbHelper.insertNotification(title, pack, text, byteArray); 
      } 
     } 

     //loop to remove notification from status bar. 
     for (int i = 0; i < whiteListModels.size(); i++) { 
      model = whiteListModels.get(i); 
      if (pack.equals(model.getPackName())) { 
       NotificationService.this.cancelNotification(sbn.getKey()); 
      } 
     } 
    } 


} 

回答

0

你可以看看這個link,但我已經用它使我不知道它爲你工作或沒有,但按照說明它提供

  • 的getId
  • 的GetNotification
  • getPackageName
  • getPostTime

從上面可以使用getPackageName打開目標應用

+0

我想在目標應用 – Raghav

+0

打開通知頁面,我不認爲這是可能 – Anil

+0

你怎麼發現至極活動,片段或其他任何他們正在用於顯示通知。 – Anil