仅在用户需要时显示 Firebase 通知

Show Firebase notification only if user wants it to

我已经通过 SharedPreferences 设置了一个选项,以便仅在用户需要时显示通知。我该如何实施?谢谢

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.i("FIREBASE", remoteMessage.getNotification().getBody());
    if (ALLOW_NOTIFICATIONS){
        //show notification
    }
    else {
        //do nothing
    }
}

我认为您无法处理您的应用程序,因为它由 Firebase SDK 自动处理。

因此,您可以使用的棘手方法是在 firebase 上使用 发送主题

例如,如果用户想要接收通知,只需使用此代码订阅主题:

FirebaseMessaging.getInstance().subscribeToTopic("AllowNotification");

如果用户不想收到通知,只需取消订阅即可。

FirebaseMessaging.getInstance().unsubscribeFromTopic("AllowNotification");