NotificationListenerService不读取堆叠通知的文本

Ben*_*ded 9 android android-notifications

我希望这不会违反任何规则,因为我已经尝试过按照"如何询问"指南.

我正在尝试使用NotificationListenerService读取传入的通知,它对我有用但只是部分.

第一个通知它的类型,让我们说 - whatsapp我可以得到自动收报机,文本和标题,但如果通知叠加我不再能读取消息的文本.

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

这是我目前实现的代码:

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

    }


}
Run Code Online (Sandbox Code Playgroud)

Bal*_*gan 5

如果您使用的是Android 7.0+,则WhatsApp使用MessageStyle扩展通知。此处-https: //developer.android.com/training/notify-user/expanded.html#message-style

从通知中检索所有5条消息,例如

MyFriend (5 messages)
testt
Run Code Online (Sandbox Code Playgroud)

做这个:

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*/

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


gab*_*osf 1

我不知道我是否误解了你,但我有一个代码可以将 Whatsapp 通知堆叠起来并在 logcat 上逐一显示,我遇到的唯一问题是当我收到第一条消息时,我的文本logcat 上显示为 null,在第一个消息之后,所有传入消息都正常工作。

\n\n

`公共类NotificationService扩展NotificationListenerService {\n上下文上下文;\n @Override

\n\n
    public void onCreate() {\n\n        super.onCreate();\n        context = getApplicationContext();\n\n    }\n    @Override\n    public void onNotificationPosted(StatusBarNotification sbn) {\n\n        String pack = sbn.getPackageName();\n        String ticker = "";\n        if (sbn.getNotification().tickerText != null) {\n            ticker = sbn.getNotification().tickerText.toString();\n        }\n        Bitmap bmp;\n        Bundle extras;\n        byte[] byteArrayS;\n        String encoded = null;\n\n        extras = sbn.getNotification().extras;\n        Log.d("extras", extras.toString());\n\n        String contato="";\n        String texto = "";\n        String search = "mensagens";\n          if((extras.getString("android.title").toLowerCase().contains(search.toLowerCase()))){\n              if(extras.getString("android.title").toLowerCase().contains("Whatsapp".toLowerCase())){\n                  extras.getString("android.title").replace("Whatsapp ","");\n                  Log.d("REPLACE","REPLACE CONCLU\xc3\x8dDO");\n              }\n\n              if((extras.getString("android.text").toLowerCase().contains(search.toLowerCase()))){\n                  Log.d("MSG1","MENSAGEM N\xc3\x83O AUTORIZADA");\n              }\n          }\n\n            //TRATA AS NOTIFICA\xc3\x87\xc3\x95ES FAZENDO COM QUE CADA MENSAGEM ENTRE DE UMA EM UMA DENTRO DA LISTA.\n            if (extras.getCharSequence("android.text") != "") {\n                if(extras.getString("android.summaryText")!= null) {\n                    contato = extras.getString("android.title");\n                    texto = extras.getCharSequence("android.text").toString();\n                    Log.d("TEXTO1", texto);\n                }\n            }\n            if(extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES) != null){\n\n                if (extras.get("android.textLines") != null) {\n                    CharSequence[] charText = (CharSequence[]) extras\n                            .get("android.textLines");\n                    Log.d("CHARTEXT",charText.toString());\n                    if (charText.length > 0) {\n                        texto = charText[charText.length - 1].toString();\n                        Log.d("TEXTO2",texto);\n                    }\n\n                }\n        }\n\n\n\n        Log.i("ContatoINTENT",contato);\n        if (texto != "") {\n            Log.i("TextoINTENT",texto);\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

`

\n