NotificationListenerService无法读取堆叠通知的文本 [英] NotificationListenerService not reading text of stacked notifications

查看:137
本文介绍了NotificationListenerService无法读取堆叠通知的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不会违反任何规则,因为我已尝试遵循如何提出要求"指南.

我正在尝试使用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 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*/

            }
        }
    }

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 it works for me but only partially.

The first notification of it's type, lets say - whatsapp I can get the ticker, text and title but then if the notifications stack up I no longer can read the text of the messages.

How do I get the text of stacked up notifications?

Here is the code I currently implement:

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

    }


}

解决方案

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

            }
        }
    }

这篇关于NotificationListenerService无法读取堆叠通知的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆