How to use the messaging function from firebase-admin

Find comprehensive JavaScript firebase-admin.messaging code examples handpicked from public code repositorys.

The firebase-admin.messaging module provides functionality for sending and receiving Firebase Cloud Messaging (FCM) messages.

92
93
94
95
96
97
98
99
100
101
        "sendername": "Nuevo reporte (USUARIO)",
        "message": 'idk',
    }
}

return admin.messaging().sendToDevice(listaTokens, payload).then((response) => {
    console.log('Se enviaron todas las notificaciones');

}).catch((err) => {
    console.log(err);
fork icon0
star icon0
watch icon1

+ 26 other calls in file

20
21
22
23
24
25
26
27
28
29
    title: title,
    body: content,
  },
  topic: topic
};
admin.messaging().send(message).then((response) => {
  return true;
}).catch((error) => {
  return false;
});
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does firebase-admin.messaging work?

The firebase-admin.messaging module provides an API for sending Firebase Cloud Messaging (FCM) messages to devices, device groups, or topics, allowing developers to send targeted notifications to specific users or groups of users. This can include sending data payloads, custom notification messages, and managing the delivery status of messages. It also provides features like token management, topic subscription management, and user segment targeting.

63
64
65
66
67
68
69
70
71
72
        `Heute ${astaEvent.start_date_details.hour}:${astaEvent.start_date_details.minutes} Uhr` 
    : `Heute ${astaEvent.start_date_details.hour}:${astaEvent.start_date_details.minutes} bis ${astaEvent.end_date_details.hour}:${astaEvent.end_date_details.minutes} Uhr`
;

// Send the message
await admin.messaging().send(
    {
        notification: {
            title: `Erinnerung ${astaEvent.title}!`,
            body: `${time} ${astaEvent.venue.venue != undefined ? `
fork icon0
star icon0
watch icon0

+ 17 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const admin = require("firebase-admin");
const serviceAccount = require("/path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://your-project-id.firebaseio.com",
});

const registrationToken = "your_device_registration_token";

const message = {
  notification: {
    title: "New message received!",
    body: "Check it out now!",
  },
  token: registrationToken,
};

admin
  .messaging()
  .send(message)
  .then((response) => {
    console.log("Notification sent successfully:", response);
  })
  .catch((error) => {
    console.error("Error sending notification:", error);
  });

This code initializes the firebase-admin SDK with the provided service account credentials and a database URL, then creates a message object containing a notification payload and the registration token of the device to which the message should be sent. Finally, it calls the send() method of the messaging() object of the admin SDK instance to send the message, logging either a success or error message depending on the outcome.