Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

android - Can't cancel Notification using NotificationListenerService

This is my service:

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");
    }
}

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.

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

P.S.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...