限制 Flutter 中特定用户的 FCM 通知

Restrict FCM Notification for a specific users in Flutter

我是 DART 的初学者。 实际问题是 "The app receives an FCM notification even if the user is logged out." 所以我想在用户注销时阻止通知。 我的问题是,"Can I check if the new notification is for the current login person or not"(使用成功登录后从服务器发送的令牌 ID)。

Can I add a check statement after receiving the notification and before showing it in the tray using a token ID

if(stored_apiToken == apiToken_sent_with_the_Notification){ ShowNotification(); }别的{ dontShowNotification(); }

提前致谢

这不是在客户端执行此操作的好方法。我们可以配置 Firebase Cloud Messaging (FCM) 来完成繁重的工作。就像我们可以让 FCM 只向我们希望它接收的设备或用户发送通知。这可以根据用例以多种方式存档,

如果您想向一组用户发送消息,您可以在 FCM 控制台上创建一个主题并将用户设为 subscribe on that topic using the firebase_messaging 库,这样针对该主题发送的通知将仅由那些用户接收订阅用户。 使用以下 link 了解它是如何完成的,并使用 firebase_messaging

应用它

Send Mssage to topic FCM

如果您想发送给特定用户, FCM 在应用程序初始启动时创建设备注册令牌。您可以通过调用 getToken() on the FCM object. firebase might refresh the token so you should listen to that using onTokenRefresh. Associate the received token with the user on the database, then you can send notification to that particular user using that token. there are multiple ways you can send the notification using AdminSDK or REST

来检索此令牌

如果您只想在用户登录时收到通知,您可以让用户在登录后订阅主题(例如 登录用户)。或者您可以添加 configure method inside an if block to check if the user is logged in (不推荐)。

如前所述,向特定用户发送消息仅在用户登录后才有效,因为我们仅在用户登录后才将设备注册令牌存储在数据库中给该特定用户英寸