Firebase messaging sendToDevice() new API?
Asked Answered
C

2

7

The "old / version 8" JavaScript API for Firebase messaging had a function for sending a notification to the mobile device:

admin.messaging().sendToDevice(...)

The "new / modular / version 9" JavaScript API fetches the messaging object with getMessaging(app), but this object does not seem to have any function, either actual or documented, to do the equivalent. I cannot find a similar function anywhere in the new API--not even in "messaging-compat", which is supposed to simulate the old API in the new one. Is it just not there? Will I have to forgo the new API entirely just to get this one function? Or perhaps is this functionality moved to some other API?

I'm trying to do this from within a Firebase Cloud Function, if that matters.

Convivial answered 18/12, 2021 at 2:3 Comment(0)
E
4

It is maddening how well written and yet horribly organized Google's documentation is.

So first of all, if you find yourself here a year and a half later, .send() is what you're looking for. It replaces .sendToDevice() And here are the two links that have the actual docs/examples:

That said, and since the deprecated API is going to be removed from firebase any day now ("June of 2024"), I thought I would point out a glaring example of where the documentation here is sketchy.

So if you navigate to the docs page suggested by @Hiranya Jayathilaka, and scroll to the method in question and click on its name: sendToDevice() - you'll be taken to a page with this at the top:

Warning: This API is now obsolete.

And if you instead read its definition:

Sends an FCM message to a single device corresponding to the provided registration token. See Send to individual devices for code samples and detailed documentation. Takes either a registrationToken to send to a single device or a registrationTokens parameter containing an array of tokens to send to multiple devices.

If you follow the "Send to individual devices" link within said definition, you'll be taken to a page with this at the top:

Caution: Sending messages (including upstream messages) with the FCM XMPP and HTTP legacy APIs was deprecated on June 20, 2023, and will be removed in June 2024. If you are using the legacy FCM send APIs, we strongly recommend that you migrate to the HTTP v1 API or consider using the Admin SDK to build send requests.

If you navigate to the "Admin SDK" link at the end of that message you'll find yourself in the midst of an explanation of how to install/enable firebase messaging api for your google cloud project - near the end of which is:

... Then, once the Firebase Admin SDK is installed, you can start writing logic to build send requests.

If you navigate to that "build send requests" link, you'll find yourself right back where you started

Evyn answered 30/5, 2024 at 4:47 Comment(0)
I
3

As far as I can see, you can just pass the token in the to property of the JSON object you pass to send like shown in this code from the documentation on sending messages to specific devices:

// This registration token comes from the client FCM SDKs.
const registrationToken = 'YOUR_REGISTRATION_TOKEN';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
getMessaging().send(message)
Iffy answered 18/12, 2021 at 2:28 Comment(6)
You are describing the old "firebase-admin" SDK. Is that not the "old / non-modular / version 8" way of doing things, or is that still an active SDK alongside the newer one ( firebase.google.com/docs/reference/js/messaging_?authuser=0 )?Convivial
getMessaging is v9, it would've been admin.messaging() before. So I'm pretty sure that documentation was updated for v9. ¯_(ツ)_/¯Iffy
The object returned by getMessaging() does not have a send() or similar method, either in the documentation or anywhere in the source code.Convivial
Hmm... interesting. I'm not sure what's happening in that case, as I must admit I also had a hard time parsing the reference documentation (which is generated from the source code so more or less guaranteed to be up to date with that code).Iffy
NodeJS seems to be the red-headed stepchild of cloud APIs. Google is actively working on and improving the Go APIs and actively neglecting JavaScript/Typescript. This is just one of many such instances I have found: I opened an issue on googleapis/nodejs-iot that hasn't been looked at for months. I'll open an issue here. Thanks for your help.Convivial
getMessaging() returns a Messaging object which contains all the usual methods including sendToDevice(): firebase.google.com/docs/reference/admin/node/…. What makes you think this class doesn't have any methods?Disembroil

© 2022 - 2025 — McMap. All rights reserved.