Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase messaging sendToDevice() new API?

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.

like image 224
Lee Daniel Crocker Avatar asked May 17 '26 09:05

Lee Daniel Crocker


1 Answers

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:

  • https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend
  • https://firebase.google.com/docs/cloud-messaging/migrate-v1#example_targeting_specific_devices

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

like image 53
Zach Pendleton Avatar answered May 19 '26 03:05

Zach Pendleton