【问题标题】:NotificationListenerService not reading text of stacked notificationsNotificationListenerService 未读取堆叠通知的文本
【发布时间】:2015-01-20 14:27:10
【问题描述】:

我希望这不会违反任何规则,因为我已尝试遵循如何提问指南。

我正在尝试使用 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) {

    }


}

【问题讨论】:

  • 我相信这是正确/默认的 Android 行为。我正在寻找资料来说明我的观点,但我看到了类似的问题:您只阅读了堆栈上的第一个通知。
  • 例如应用程序 airdroid 使用相同的 notificationListenerService 方法,它可以正确读取通知,所以我确定有一种方法我只是不知道
  • 你找到答案了吗?
  • 根据文档。这是正确的行为。只会通知 FIRST/REMOVE 通知。
    developer.android.com/reference/android/service/notification/… 抱歉,我没有找到任何获取通知更新的方法

标签: android android-notifications


【解决方案1】:

如果您使用的是 Android 7.0+,WhatsApp 使用 MessageStyle 扩展通知。这里-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*/

            }
        }
    }

【讨论】:

    【解决方案2】:

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

    `公共类 NotificationService 扩展 NotificationListenerService { 上下文上下文; @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);
            }
    

    `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多