Best approach to FCM with tokens and a Cloud Function

193

Solution 1

Unfortunately due to how sending to individual devices with Firebase works, you do need that registration token as you've said.

When you say "within the Authentication", does that mean you have another collection in firebase called "Authentication", or are you talking about saving into the user's Firebase Authentication claims? I'd strongly advise against the latter as that gets put into the auth token.

What I'd recommend is making a separate collection in Firebase keyed on the UID, and saving the registration token there. That could be called "Authentication" or "Users" or whatever makes the most sense to you, and that would be where I would store any user-specific data.

Also, for your events I think you'd probably want to have a structure like events/${User-ID}/${Event-ID}/.

Solution 2

You need to save the token to the CloudFirestore database when the user opens the app every time. Then in the Cloud functions query the token and use code like this to send the notification to the specific user:

//here you will query the specific user to get the token

    var token = //get the token from Firestore

    var payload = admin.messaging.MessagingPayload = {
    
                notification: {
                   title: senderName,
                   body: message,
                   icon: 'your-icon-url',
                   click_action: 'FLUTTER_NOTIFICATION_CLICK',
                },
    
                "data": {
                   "sender": sender,
                   "senderName": senderName,
                   "gender": gender,
                   "fullName": fullName,
                   "message": message
                 }
    
       admin.messaging().sendToDevice(
                token,
                payload,
              
    
                {
                   contentAvailable: true,
                   priority: "high",
                },
                 );
Share:
193
cc976a
Author by

cc976a

Updated on December 19, 2022

Comments

  • cc976a
    cc976a over 1 year

    I have an app, with user authentication through an email and password - which all works well.

    Through the use of the app the user will enter various dates, and on these dates I want to send a notification to that user (e.g. their birthday).

    At the moment I store in to a Collection (let's say 'dateEntires') the users UID and the date they enter. I am trying to write a cloud function for a cron job to check the Collection every day and if a date matches today, then send the user (through their UID) a notification. The issue I have is with registration tokens.

    I have read that I need to send the notification to the user based on their registration token. This I could probably set up - but my question is what happens if their registration token changes (i.e. they uninstall and reinstall the app)?

    In order to achieve this should I?

    1. Store the registration token within the Authentication, check this each time the user opens the app and update where necessary - and the Cloud Function can check the Collection and if a date does match today, then it will use the UID in the Collection to find the Registration Token in the Authentication table to send the message

    2. Store the Registration Token and UID in to dateEntries and each time the user opens the app, check the Registration Token in all records in 'dateEntries' for this users UID and update where needed

    Or is there a better approach to this?

    It would be much easier to send the personalised notifications based on their (fixed) UID rather than their (variable) Registration Token - but I've been told this isn't possible (?)

  • cc976a
    cc976a about 3 years
    Thanks - and yes this was my thinking as a 3rd option too. Add a separate Collection just storing the UID and the Token - and then use this as part of the Cloud Function for sending the notification. Much easier maintaining one user data Collection, and then a separate Collection for all the actual data - and calling both as part of the function. Thanks!