NotificationListenerService not reading text of stacked notifications

后端 未结 2 1242
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 10:59

I hope this doesn\'t violate any rule as I\'ve tried following the How-to-ask guide.

I\'m trying to read incoming notifications using NotificationListenerService and

相关标签:
2条回答
  • 2020-12-29 11:24

    If you're working with Android 7.0+, WhatsApp uses MessageStyle Expanded Notifications. Here - https://developer.android.com/training/notify-user/expanded.html#message-style

    To retrieve all 5 messages from a notification like

    MyFriend (5 messages)
    testt
    

    Do this:

    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*/
    
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-29 11:30

    I dont know if i missunderstood you, but i've a code that get's whatsapp notifications stacked and shows on logcat one by one, only problem that i'm having is that when i receive the first message, my text on logcat shows me null, and after the first, all incoming messages is working.

    `public class NotificationService extends NotificationListenerService { Context context; @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 讨论(0)
提交回复
热议问题