Restrict Fcm Notification For A Specific Users In Flutter
Solution 1:
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,
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
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).
Sending message to a specific user as i mentioned earlier will work only after the user is logged in because we are storing the device registration token on the database to that particular user only after the user is logged in.
Post a Comment for "Restrict Fcm Notification For A Specific Users In Flutter"