0

I am developing a React Native app and I using Firebase push notification service. I have users and all users have their own user token, I want to use these user tokens for send push notification.

First I thought, when user login to app I can register the user token to topic but I'm not sure about that too.

So is there any way to use custom token for send push notification to specific user?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

1

Firebase Cloud Messaging doesn't have the concept of a user. All it knows it the so called FCM token, which identifies a specific installed app on a specific device.

If you're using the react-native-firebase library, you can learn how to get the FCM token from the documentation on saving tokens. Once you have the token, you can us the token to send a message to that specific app on that specific device.

If you want to target a user across any devices where they are using (and signed in to) your app, you'll need to relate the tokens of those devices to a user yourself. In the code example linked above that is done with:

firestore()
    .collection('users')
    .doc(userId)
    .update({
      tokens: firestore.FieldValue.arrayUnion(token),
    });

This code saves the token to Cloud Firestore, but it would work the same on any database: this associates the FCM token (token) with the current user (userId) in a way that allows multiple tokens to be stored per user. Then when you want to send a message to a user, you look up the tokens for that user and call FCM to send to all those tokens.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank's for your answering. But anyway I have to use extra service for check FCM token. I just wondered maybe I can register and I can use which token that I have. But I just wondering something, if I subscribe all each user for unique topic, how it will be? For example, If I will subscribe to all each user to unique topic and this topic will be their user token which already I have, and than can't I use this for send notification? – Uraz Akgültan Aug 08 '20 at 16:13
  • If you check tis [blog post](https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html) that is linked in the question, you'll see an approach that uses the UID of a user as a topic. The only concern there is that anyone can send any message they want to a user when they know their UID. Whether that is acceptable is something only you can determine. – Frank van Puffelen Aug 08 '20 at 17:11
0

you can send notification for a specific user:

'to' => *users token*,
'notification' => [
        'body' =>  $message,
        title' => $title,
                    
],
  • I know this way. I want that when user create account in my app, I assign specific token for them. I want to use this code for send push notification. – Uraz Akgültan Aug 08 '20 at 12:20
  • i dont understand why u want to assign a specific token if firebase already give the user token using: let fcmToken = await firebase.messaging().getToken();, just take this token and save it and whenever u want u cant send using this token – Ahmad Abou Saleh Aug 08 '20 at 12:26
  • I don't want keep extra data. I have already token for all user and I don't want to use extra token for this. – Uraz Akgültan Aug 08 '20 at 12:53