2

Trying to send remote push notifications through firebase cloud functions. Resources I've been following achieves this through sendToDevice method, which takes a String as an argument. A resource from GitHub says its a "device notification token" that is retrieved when user agrees to receive notifications in app. Firebase says its a "registration token that comes from the client FCM SDKs". What should be the input here, and how to I retrieve it?

      // Send notification to device via firebase cloud messaging.  
      // https://firebase.google.com/docs/cloud-messaging/admin/send-messages
      // https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js
      // 

      admin.messaging().sendToDevice(request.query.tokenId, payload).then(response => {
        response.results.forEach((result, index) =>  {
            const error = result.error 
            if (error) {
                console.log("Failure sending notification.")
            }
        });
    }); 

1 Answers1

4

You need to integrate FCM into your iOS app. Pay attention to the part about receiving the current registration token.

Registration tokens are delivered via the FIRMessagingDelegate method messaging:didReceiveRegistrationToken:. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:

  • If the registration token is new, send it to your application server (it's recommended to implement server logic to determine whether the token is new).
  • Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.

So, you'll have to get a hold of this token in your app, store it somewhere that the Cloud Function can get a hold of (traditionally, Realtime Database), and query for it at the time the function runs.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I believe that the same steps are necessary for Android client following this link: https://firebase.google.com/docs/cloud-messaging/android/client or is there any differences one should be aware of? – Red M Nov 14 '18 at 15:15
  • I don't think so. – Doug Stevenson Nov 14 '18 at 16:53
  • could you explain in depth this section [ receiving the current registration token.] I do not understand it –  May 27 '19 at 20:03
  • Does this registration token expire? If I store it to database & read from there for notification, will it change later? – akshay bhange Jun 25 '19 at 09:18