user6354073
user6354073

Reputation:

Push notifications using Firebase Cloud Function iOS

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.")
            }
        });
    }); 

Upvotes: 2

Views: 3072

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

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.

Upvotes: 4

Related Questions