Restrict FCM Notification for a specific users in Flutter

4,103

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.

Share:
4,103
ABHIMANGAL MS
Author by

ABHIMANGAL MS

Updated on December 16, 2022

Comments

  • ABHIMANGAL MS
    ABHIMANGAL MS over 1 year

    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

    • Doug Stevenson
      Doug Stevenson over 4 years
      How are you going to identify the recipient in the sent message? That seems necessary.
    • ABHIMANGAL MS
      ABHIMANGAL MS over 4 years
      onMessage: (Map<String, dynamic> message) async{ print('on message $message'); }, // ignore: missing_return onResume: (Map<String, dynamic> message) async{ print('onResume $message'); }, // ignore: missing_return onLaunch: (Map<String, dynamic> message) async{ print('onLaunch $message'); },
    • ABHIMANGAL MS
      ABHIMANGAL MS over 4 years
      using onMessage, onResume and onLaunch
    • ABHIMANGAL MS
      ABHIMANGAL MS over 4 years
      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.
  • ABHIMANGAL MS
    ABHIMANGAL MS over 4 years
    Thanks for the information. So how can we delete the fcm token after the user is logged out for a pardticular device. ?
  • Arjunraj kokkadan
    Arjunraj kokkadan over 4 years
    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. you just need to overwrite the fcm token on the database when the user logs in again.
  • ABHIMANGAL MS
    ABHIMANGAL MS over 4 years
    deleteInstance() method for fcm will delete the current fcm token. thank you for the information.