0

I want clear all notifications of notification bar and had made a separated class that extends NotificationListenerService

package com.testando.teste;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

public class NotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        cancelNotification(sbn.getKey());
    }
}

and my manifest file is like this:

<application>

<service android:name=".NotificationListener"
            android:enabled="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

 </application>

Then i want know why every time that a notification arrives, this not is removed?

Or then some one know some way of remove these notification without use of NotificationListenerService class, similar to "Clear All" button?

  • Are you sure you enabled the app access to notifications from within system settings? – Pawel Aug 09 '18 at 23:24
  • @Pawel, then this can be the reason to nothing is working. How make this programatically? –  Aug 09 '18 at 23:27
  • You can only open settings activity like [this](https://stackoverflow.com/a/32368604/9241978). It is impossible to grant this permission without user explicit action. – Pawel Aug 09 '18 at 23:31
  • @Pawel,my app is unlocked to notifications and even so still not works. My last attempt was only [**this code**](https://stackoverflow.com/a/13676534/9672569) without nothing on manifest related to **Notifications**. –  Aug 10 '18 at 02:08

1 Answers1

1
// Clears notification tray messages
    public static void clearNotifications(Context context) {
        try{
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancelAll();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

AndroidHive has a detail tutorial for Notifications with

Firebase Cloud Messaging

This tutorial is your reference.I hope this will help for you.

Htoo Aung Hlaing
  • 2,173
  • 15
  • 26
  • i need of a effect where i not need click over notification, want make equals to "Clear All" button. This is possible? –  Aug 10 '18 at 16:52
  • Form example, [**this way**](https://angularfirebase.com/images/native-ionic-fcm.gif) –  Aug 10 '18 at 17:00
  • 1
    yes, you can , it is work as "Clear All" button. You should try with code – Htoo Aung Hlaing Aug 12 '18 at 08:47