1

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

user7418129
  • 1,074
  • 14
  • 18
  • How are you going to identify the recipient in the sent message? That seems necessary. – Doug Stevenson Dec 07 '19 at 05:36
  • `onMessage: (Map message) async{ print('on message $message'); }, // ignore: missing_return onResume: (Map message) async{ print('onResume $message'); }, // ignore: missing_return onLaunch: (Map message) async{ print('onLaunch $message'); },` – user7418129 Dec 07 '19 at 07:04
  • using onMessage, onResume and onLaunch – user7418129 Dec 07 '19 at 07:05
  • What I actually do is, 1. I gets an FCm token while the app is opened. (FCM token will change after uninstalling the app). 2. When I call the LoginApi, I pass some credentials along with FCMToken. 3. The Backe-end gives an **API key** as the response. (I storeit inside the SharedPreferences) 4. And the notification is sent from the server using the FCM-token that I already have given to the server while login. 5. That FCMToken also contains the same **API key**. **So is there any way to check whether an incoming FCM notification is for userA or userB by any comparison or something.** – user7418129 Dec 07 '19 at 07:31

1 Answers1

3

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

Send Mssage to topic FCM

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.

  • 1
    Thanks for the information. So how can we delete the fcm token after the user is logged out for a pardticular device. ? – user7418129 Dec 07 '19 at 08:39
  • 1
    You don't have to, the fcm token is not unique for each device instead unique for each app instance, even clearing the data of the app removes the token and a new fcm token will be created on starting the app again. more on it here [link](https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html?authuser=0). you just need to overwrite the fcm token on the database when the user logs in again. – Arjunraj kokkadan Dec 07 '19 at 19:28
  • 1
    deleteInstance() method for fcm will delete the current fcm token. thank you for the information. – user7418129 Dec 12 '19 at 07:31