无法使用NotificationListenerService取消通知 [英] Can't cancel Notification using NotificationListenerService

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

问题描述

这是我的服务:

public class Listener extends NotificationListenerService {
    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 = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();

        Log.i("Msg",pack);
        Log.i("Msg",ticker);
        Log.i("Msg",title);
        Log.i("Msg",text);

        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);
            LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
        Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_LONG).show();

 //final String T=  sbn.getKey();

        Listener.this.cancelAllNotifications();
    }

    @Override

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

为了进行测试,我想在它们到达后立即从任何应用程序中取消所有通知.我在onNotificationPosted内部调用了Listener.this.cancelAllNotifications();,但是它不起作用.

For testing, I would like to DISMISS ALL NOTIFICATIONS FROM ANY APP as soon as they arrive. I have called Listener.this.cancelAllNotifications(); inside onNotificationPosted, but it does not work.

我在做什么错?我正在android 5.0.2版上对此进行测试.

What am I doing wrong ? I am testing this on android version 5.0.2.

P.S.

正如Android提到的,我已经添加了权限以及意图过滤器.

I HAVE added the permissions along with the intent filter, as android mentions.

<service android:name=".NotificationListener"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

推荐答案

对于实现您的 NotificationListenerService 的应用,确保通知访问"处于活动状态(选中).

Make sure the 'Notification Access' is active (checked) for the app implementing your NotificationListenerService.

可以在设置"菜单中进行验证.根据Android版本的不同,它位于两个地址中的任意一个.对于Android 5.0,它应位于: 设置->声音和声音;通知->通知访问

It can be verified in 'Settings' menu. It is at either of the addresses depending upon the Android version. For Android 5.0, it should be at: Settings -> Sound & notification -> Notification Access

否则,您可以在以下位置找到: 设置->安全->通知访问

Or else, you may find at: Settings -> Security -> Notification Access

尝试为您的应用启用它,然后尝试清除通知.

Try enabling it for your app and then try to clear the notifications.

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

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