在Flutter中限制特定用户的FCM通知 [英] Restrict FCM Notification for a specific users in Flutter

查看:61
本文介绍了在Flutter中限制特定用户的FCM通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是DART的初学者.实际问题是即使用户注销,该应用程序也会收到FCM通知."因此,如果用户已注销,我想阻止通知.我的问题是,我可以检查新通知是否针对当前登录者"(成功登录后使用从服务器发送的令牌ID).

I am a beginner in DART. Actual problem is that "The app receives an FCM notification even if the user is logged out." So I want to prevent the notification if the user is logged out. My question is, "Can I check if the new notification is for the current login person or not" (Using a token ID sent from the server after a successful login).

我可以在收到通知后添加检查语句吗?在使用令牌ID将其显示在托盘中之前

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

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

if(stored_apiToken == apiToken_sent_with_the_Notification){ ShowNotification(); }else{ dontShowNotification(); }

预先感谢

推荐答案

在客户端执行此操作不是一个好方法.我们可以配置Firebase Cloud Messaging(FCM)来完成繁重的工作.就像我们可以使FCM仅将通知发送给我们希望其接收的设备或用户一样.可以根据使用情况以多种方式将其存档,

It is not a good way to do it on the client side. We can configure Firebase Cloud Messaging(FCM) to do the heavy lifting. Like we can make the FCM to send notifications to only the devices or users we want it to receive. This can be archived in many ways depending on the use case,

如果要将消息发送给一组用户,则可以在FCM控制台上创建主题,并使用户 firebase_messaging firebase_messaging 包

If you want to send message to a group of users, you can create a topic on the FCM console and make the users subscribe on that topic using the firebase_messaging library, so the notification sent for that topic will be received only by those subscribed users. use the following link to understand how its done and apply it using the firebase_messaging package

将消息发送到主题FCM

如果要发送给特定用户,FCM在应用程序的初次启动时创建设备注册令牌.您可以通过在以下网址上调用 getToken()来检索此令牌.FCM对象.firebase可能会刷新令牌,因此您应该使用 onTokenRefresh 收听>.将接收到的令牌与数据库上的用户相关联,然后您可以 AdminSDK REST

If you want to send to a specific user, FCM creates a device registration token at the initial startup of the app. you can retrieve this token by calling 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

如果只希望在用户登录时收到通知,则可以让用户在用户登录后订阅主题(例如已登录的用户),也可以添加配置方法,以检查用户已登录(不推荐).

if you only want to receive notification when the user is logged in you can make a user to subscribe to a topic (like logged in users) after they log in. or you can add the configure method inside an if block to check if the user is logged in (not recommended).

我之前提到的向特定用户发送消息仅在该用户登录后才有效,因为只有在该用户登录后,我们才会在该特定用户的数据库中存储设备注册令牌.

这篇关于在Flutter中限制特定用户的FCM通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆