NotificationListenerService无法读取堆叠通知的文本

13

我希望这不会违反任何规定,因为我已经尽力按照如何提问的指南操作。

我正在尝试使用NotificationListenerService读取传入通知,它对我有效,但只有部分地有效。

它的类型是第一个通知,比如-whatsapp,我可以获取ticker、文本和标题,但是如果通知堆积起来,我就再也无法读取消息的文本了。

如何获取堆积的通知的文本?

这是我目前实现的代码:

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

    }


}

我相信这是正确/默认的Android行为。我正在寻找支持我的观点的来源,但我看到了类似的问题:你只读取堆栈上第一个通知。 - shkschneider
例如,应用程序Airdroid使用相同的NotificationListenerService方法,并且可以正确读取通知,因此我确信有一种方法,只是我不知道而已。 - Bended
你找到答案了吗? - user1406716
根据文档,这是正确的行为。只有第一个/删除通知将被通知。<br />https://developer.android.com/reference/android/service/notification/NotificationListenerService.html不好意思,但我找不到任何获取通知更新的方法。 - Sulfkain
2个回答

8
如果您正在使用Android 7.0+,WhatsApp将使用MessageStyle Expanded Notifications。请参阅以下链接:https://developer.android.com/training/notify-user/expanded.html#message-style 要从像这样的通知中检索所有5条消息,请执行以下操作:
MyFriend (5 messages)
testt

做这个:
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*/

            }
        }
    }

1
我不知道是否误解了你的意思,但我有一个代码可以堆叠并在logcat上逐个显示WhatsApp通知,唯一的问题是当我收到第一条消息时,我的日志文本显示为空,而在第一条消息之后,所有传入的消息都可以正常工作。
`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);
        }

`


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接