0

I am working with FCM, and as per documentation, "the notification is delivered to the device’s system tray". So how to attach a pending intent or register a broadcast/callback when a notification message is dismissed by the user?

Is there any way of doing this? Or I have to use data messages and build notification manually with pending intent to receive a broadcast if the notification is dismissed?

EDIT - Solution

public class NotificationListener extends NotificationListenerService {
    private boolean isSafeToExecute, isNotificationAccessEnabled;



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

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
          Log.e("removed", sbn.getNotification().getClass().getSimpleName());
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        isSafeToExecute = true;
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
        isSafeToExecute = false;
    }
    @Override
    public IBinder onBind(Intent mIntent) {
        IBinder mIBinder = super.onBind(mIntent);
        isNotificationAccessEnabled = true;
        return mIBinder;
    }

    @Override
    public boolean onUnbind(Intent mIntent) {
        boolean mOnUnbind = super.onUnbind(mIntent);
        isNotificationAccessEnabled = false;
        return mOnUnbind;
    }
}

The above one works. I have previously not overriden onBind and OnUnbind that caused the problem. Ask the user to grant notification access if not granted, this link would help. Check if user has granted NotificationListener access to my app

Community
  • 1
  • 1
Debanjan
  • 2,817
  • 2
  • 24
  • 43

1 Answers1

1

You should go with the onNotificationRemoved (StatusBarNotification sbn)

This will let you catch in cases where the user has removed the notification or the andorid system has withdrawn the notification.

Ashish Kumar
  • 374
  • 4
  • 11
  • Hi, have you used the NotificationListenerService with FCM notification messages? – Debanjan Aug 22 '16 at 13:05
  • Yes, check this link out for more details, https://developer.android.com/reference/android/service/notification/NotificationListenerService.html – Ashish Kumar Aug 23 '16 at 05:27
  • I have tried it, but I am not getting notified when the fcm notification is dismissed or onNotificationPosted. Do we have to start the service manually using startService() or the intent will do it? I have done exactly as the link. One more thing, when I go to Settings->Security->Notification access, It is not showing my app, instead it is showing "No applications on this device are capable of reading notifications" – Debanjan Aug 23 '16 at 06:29
  • Can you post the code, so that the community could help you more? – Ashish Kumar Aug 23 '16 at 08:20