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