2015-01-20 15 views
7

Tôi hy vọng điều này không vi phạm bất kỳ quy tắc nào mà tôi đã thử theo hướng dẫn Cách thực hiện.NotificationListenerService không đọc văn bản thông báo xếp chồng lên nhau

Tôi đang cố gắng đọc thông báo đến bằng cách sử dụng NotificationListenerService và nó hoạt động cho tôi nhưng chỉ một phần.

Thông báo đầu tiên về loại đó, cho phép nói - whatsapp tôi có thể nhận được mã, văn bản và tiêu đề nhưng sau đó nếu thông báo chồng lên tôi không còn có thể đọc nội dung thư.

Làm cách nào để nhận được văn bản của thông báo xếp chồng lên nhau?

Đây là mã tôi hiện đang thực hiện:

public class NotificationService extends NotificationListenerService { 

    private Context context; 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     context = getApplicationContext(); 

    } 

    @Override 
    public void onNotificationPosted(StatusBarNotification sbn) { 
     String pack = sbn.getPackageName(); 
     String ticker = sbn.getNotification().tickerText.toString(); 
     Bundle extras = sbn.getNotification().extras; 
     String title = ""; 
     String text = ""; 


     if (extras.containsKey("android.title")) { 
      title = extras.getString("android.title"); 
     } 

     if (extras.containsKey("android.text")) { 
      if (extras.getCharSequence("android.text") != null) { 
       text = extras.getCharSequence("android.text").toString(); 
      } 
     } 
     if (pack != null) { 
      Log.i("Package", pack); 
     } 

     if (ticker != null) { 
      Log.i("ticker", ticker); 
     } 

     if (title != null) { 
      Log.i("Title", title); 
     } 

     if (text != null) { 
      Log.i("Text", text); 
     } 


    } 

    @Override 
    public void onNotificationRemoved(StatusBarNotification sbn) { 

    } 


} 
+0

Tôi tin rằng đây là/hành vi Android mặc định chính xác. Tôi đang tìm kiếm các nguồn để làm cho quan điểm của mình nhưng tôi thấy các vấn đề tương tự: bạn chỉ đọc thông báo đầu tiên trên stack. – shkschneider

+0

ứng dụng airdroid chẳng hạn sử dụng cùng một phương thức notificationListenerService và nó đọc thông báo chính xác vì vậy tôi chắc chắn có một cách mà tôi chỉ không nhận thức được – Bended

+0

Bạn có tìm thấy câu trả lời cho điều này không? – user1406716

Trả lời

0

Tôi không biết nếu tôi missunderstood bạn, nhưng tôi đã là một mã mà có được của WhatsApp thông báo xếp chồng lên nhau và hiển thị trên logcat từng người một, chỉ có vấn đề mà tôi là có khi tôi nhận được tin nhắn đầu tiên, văn bản của tôi trên logcat cho thấy tôi null, và sau khi đầu tiên, tất cả các tin nhắn đến là làm việc.

`lớp công khai NotificationService mở rộng NotificationListenerService { Ngữ cảnh bối cảnh; @ Override

public void onCreate() { 

     super.onCreate(); 
     context = getApplicationContext(); 

    } 
    @Override 
    public void onNotificationPosted(StatusBarNotification sbn) { 

     String pack = sbn.getPackageName(); 
     String ticker = ""; 
     if (sbn.getNotification().tickerText != null) { 
      ticker = sbn.getNotification().tickerText.toString(); 
     } 
     Bitmap bmp; 
     Bundle extras; 
     byte[] byteArrayS; 
     String encoded = null; 

     extras = sbn.getNotification().extras; 
     Log.d("extras", extras.toString()); 

     String contato=""; 
     String texto = ""; 
     String search = "mensagens"; 
      if((extras.getString("android.title").toLowerCase().contains(search.toLowerCase()))){ 
       if(extras.getString("android.title").toLowerCase().contains("Whatsapp".toLowerCase())){ 
        extras.getString("android.title").replace("Whatsapp ",""); 
        Log.d("REPLACE","REPLACE CONCLUÍDO"); 
       } 

       if((extras.getString("android.text").toLowerCase().contains(search.toLowerCase()))){ 
        Log.d("MSG1","MENSAGEM NÃO AUTORIZADA"); 
       } 
      } 

      //TRATA AS NOTIFICAÇÕES FAZENDO COM QUE CADA MENSAGEM ENTRE DE UMA EM UMA DENTRO DA LISTA. 
      if (extras.getCharSequence("android.text") != "") { 
       if(extras.getString("android.summaryText")!= null) { 
        contato = extras.getString("android.title"); 
        texto = extras.getCharSequence("android.text").toString(); 
        Log.d("TEXTO1", texto); 
       } 
      } 
      if(extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES) != null){ 

       if (extras.get("android.textLines") != null) { 
        CharSequence[] charText = (CharSequence[]) extras 
          .get("android.textLines"); 
        Log.d("CHARTEXT",charText.toString()); 
        if (charText.length > 0) { 
         texto = charText[charText.length - 1].toString(); 
         Log.d("TEXTO2",texto); 
        } 

       } 
     } 



     Log.i("ContatoINTENT",contato); 
     if (texto != "") { 
      Log.i("TextoINTENT",texto); 
     } 

'

0

Nếu bạn đang làm việc với Android 7.0 trở lên, WhatsApp sử dụng Notifications MessageStyle mở rộng. Ở đây - https://developer.android.com/training/notify-user/expanded.html#message-style

Để lấy tất cả 5 tin nhắn từ một thông báo như

MyFriend (5 messages) 
testt 

Làm điều này:

Bundle extras = mysbn.getNotification().extras; 
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)){ 
     Parcelable b[] = (Parcelable[]) extras.get(Notification.EXTRA_MESSAGES); 

     if(b != null){ 
      content = ""; 
      for (Parcelable tmp : b){ 

       Bundle msgBundle = (Bundle) tmp; 
       content = content + msgBundle.getString("text") + "\n"; 

       /*Set<String> io = msgBundle.keySet(); // To get the keys available for this bundle*/ 

      } 
     } 
    } 
Các vấn đề liên quan