如何在用户解除fcm通知消息时获得广播或回叫?

时间:2016-08-22 09:58:59

标签: android firebase push-notification android-broadcast firebase-cloud-messaging

我正在使用FCM,根据文档,“通知已发送到设备的系统托盘”。那么当用户解除通知消息时,如何附加待处理的意图或注册广播/回调?

有没有办法做到这一点?或者我必须手动使用数据消息并手动构建通知,以便在通知被解除时接收广播?

编辑 - 解决方案

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

以上一项有效。我以前没有覆盖导致问题的onBind和OnUnbind。 如果未授权,请用户授予通知访问权限,此链接将有所帮助。 Check if user has granted NotificationListener access to my app

1 个答案:

答案 0 :(得分:1)

你应该使用onNotificationRemoved(StatusBarNotification sbn)

这可以让您了解用户已删除通知或andorid系统撤消通知的情况。