Android Studio NotificationListenerService 不适用于 YouTube 通知

问题描述

我在使用 Youtube 通知和使用 NotificationListenerService 的 android 应用程序时遇到问题。在过去的几个月里,我在收听来自其他应用程序(例如 twitter、discord 和 facebook)的通知时没有遇到任何问题。但是,我一直在尝试侦听 Youtube 通知并且应用程序停止工作。我注意到它从 Youtube 通知返回的标题和文本始终为空,而其他通知则从未如此。想知道是否有人对此有所了解或在 Youtube 或任何其他 Google 通知中遇到过这种情况?

这里是我用来参考的代码


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {

    Context context;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {
        String pack = sbn.getPackageName();
        if (!TextUtils.isEmpty(pack)) {
            String ticker = "";

            if (sbn.getNotification().tickerText != null) {
                ticker = sbn.getNotification().tickerText.toString();
            }
            Bundle extras = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                extras = sbn.getNotification().extras;
            }
            String title = extras.getString("android.title");
            String text = extras.getCharSequence("android.text").toString();
            int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
            Bitmap id = sbn.getNotification().largeIcon;

            Log.i("Package",pack);
            Log.i("Ticker",ticker);
            Log.i("Title",title);
            Log.i("Text",text);

            Intent msgrcv = new Intent("Msg");
            msgrcv.putExtra("package",pack);
            msgrcv.putExtra("ticker",ticker);
            msgrcv.putExtra("title",title);
            msgrcv.putExtra("text",text);
            if (id != null) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                id.compress(Bitmap.CompressFormat.PNG,100,stream);
                byte[] byteArray = stream.toByteArray();
                msgrcv.putExtra("icon",byteArray);
            }
            LocalbroadcastManager.getInstance(context).sendbroadcast(msgrcv);
        }
        
    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)