Reputation: 1114
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).
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();
}else{
dontShowNotification();
}
Thanks in advance
Upvotes: 1
Views: 4727
Reputation: 488
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.
Upvotes: 3