当用户关闭 fcm 通知消息时如何获得广播或回调?

How to get a broadcast or callback when a fcm notification message is dismissed by user?

我正在使用 FCM,根据文档,"the notification is delivered to the device’s system tray"。那么当通知消息被用户关闭时,如何附加一个挂起的意图或注册一个broadcast/callback?

有什么办法吗?或者,如果通知被取消,我必须使用数据消息并手动构建通知以等待接收广播?

编辑 - 解决方案

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。 如果未授予通知访问权,请要求用户授予通知访问权,这 link 会有所帮助。 Check if user has granted NotificationListener access to my app

你应该使用 onNotificationRemoved (StatusBarNotification sbn)

这将让您捕捉到用户已删除通知或 andorid 系统已撤回通知的情况。