admin管理员组

文章数量:1122832

I am currently using Firebase Cloud Functions to send notifications via Firebase Cloud Messaging (FCM). Notifications are being successfully sent, as confirmed by the logs in the Cloud Function and the "Reports Delivery" section in FCM. However, I can't figure out if it's possible to view the actual notifications (content and data) somewhere else besides these two sources.

Additionally, when I check FCM, I see that notifications are being sent but not received. I have the following questions:

Is there any way to view the sent notifications directly in Firebase (like the campaign section) or another tool, instead of only checking the logs or FCM reports?

Here are some details of my implementation:

Notifications are sent to a specific topic using Cloud Functions.

The Cloud Function logs confirm successful execution, as shown in the attached screenshot. FCM reports show that the notifications are sent but with 0 received/impressions. I have attached a screenshot of the FCM delivery report and Cloud Function logs for reference.

Cloud Function code:

import * as admin from "firebase-admin";
import {onDocumentCreated} from "firebase-functions/v2/firestore";
import {getMessaging} from "firebase-admin/messaging";

// Initialize Firebase Admin SDK
admin.initializeApp();

// Firestore trigger for when a notification is created
export const onNotificationCreate = onDocumentCreated(
  "notifications/{notificationId}",
  async (event) => {
    const snap = event.data;

    if (!snap) {
      console.error("No snapshot data found");
      return;
    }

    const notificationData = snap.data();

    if (!notificationData) {
      console.error("No notification data found");
      return;
    }

    // Construct the message payload for Firebase Cloud Messaging
    const message = {
      notification: {
        title: notificationData.title || "No title",
        body: notificationData.body || "No body",
      },
      topic: notificationData.topic,
    };

    console.log(message);
    try {
      // Send the message
      const response = await getMessaging().send(message);
      console.log("Successfully sent message:", response);

      // Log success in Firestore
      await admin.firestore().collection("notificationLogs").add({
        notificationId: event.params.notificationId,
        topic: notificationData.topic,
        title: notificationData.title,
        body: notificationData.body,
        status: "Sent",
        response: response,
        timestamp: admin.firestore.FieldValue.serverTimestamp(),
      });
    } catch (error) {
      console.error("Error sending message:", error);

      // Log error in Firestore
      await admin.firestore().collection("notificationLogs").add({
        notificationId: event.params.notificationId,
        topic: notificationData.topic,
        title: notificationData.title,
        body: notificationData.body,
        status: "Error",
        error: error,
        timestamp: admin.firestore.FieldValue.serverTimestamp(),
      });
    }
  }
);

What steps can I take to debug this issue further? Is there a way to test and verify the notifications more effectively?

Thanks in advance for your help!

FCM reportsCloud Function Logs

I am currently using Firebase Cloud Functions to send notifications via Firebase Cloud Messaging (FCM). Notifications are being successfully sent, as confirmed by the logs in the Cloud Function and the "Reports Delivery" section in FCM. However, I can't figure out if it's possible to view the actual notifications (content and data) somewhere else besides these two sources.

Additionally, when I check FCM, I see that notifications are being sent but not received. I have the following questions:

Is there any way to view the sent notifications directly in Firebase (like the campaign section) or another tool, instead of only checking the logs or FCM reports?

Here are some details of my implementation:

Notifications are sent to a specific topic using Cloud Functions.

The Cloud Function logs confirm successful execution, as shown in the attached screenshot. FCM reports show that the notifications are sent but with 0 received/impressions. I have attached a screenshot of the FCM delivery report and Cloud Function logs for reference.

Cloud Function code:

import * as admin from "firebase-admin";
import {onDocumentCreated} from "firebase-functions/v2/firestore";
import {getMessaging} from "firebase-admin/messaging";

// Initialize Firebase Admin SDK
admin.initializeApp();

// Firestore trigger for when a notification is created
export const onNotificationCreate = onDocumentCreated(
  "notifications/{notificationId}",
  async (event) => {
    const snap = event.data;

    if (!snap) {
      console.error("No snapshot data found");
      return;
    }

    const notificationData = snap.data();

    if (!notificationData) {
      console.error("No notification data found");
      return;
    }

    // Construct the message payload for Firebase Cloud Messaging
    const message = {
      notification: {
        title: notificationData.title || "No title",
        body: notificationData.body || "No body",
      },
      topic: notificationData.topic,
    };

    console.log(message);
    try {
      // Send the message
      const response = await getMessaging().send(message);
      console.log("Successfully sent message:", response);

      // Log success in Firestore
      await admin.firestore().collection("notificationLogs").add({
        notificationId: event.params.notificationId,
        topic: notificationData.topic,
        title: notificationData.title,
        body: notificationData.body,
        status: "Sent",
        response: response,
        timestamp: admin.firestore.FieldValue.serverTimestamp(),
      });
    } catch (error) {
      console.error("Error sending message:", error);

      // Log error in Firestore
      await admin.firestore().collection("notificationLogs").add({
        notificationId: event.params.notificationId,
        topic: notificationData.topic,
        title: notificationData.title,
        body: notificationData.body,
        status: "Error",
        error: error,
        timestamp: admin.firestore.FieldValue.serverTimestamp(),
      });
    }
  }
);

What steps can I take to debug this issue further? Is there a way to test and verify the notifications more effectively?

Thanks in advance for your help!

FCM reportsCloud Function Logs

Share Improve this question edited Nov 21, 2024 at 14:06 Frank van Puffelen 598k84 gold badges887 silver badges858 bronze badges Recognized by Google Cloud Collective asked Nov 21, 2024 at 10:59 KevinKevin 1
Add a comment  | 

1 Answer 1

Reset to default 0

Is there any way to view the sent notifications directly in Firebase (like the campaign section) or another tool, instead of only checking the logs or FCM reports?

The Firebase console and the data API only show aggregated data, as the amount of data it'd need to maintain to view individual message details in the Firebase console would be prohibitive.

To get full details on individual messages, you can configure FCM to export its delivery data to BigQuery. There you can then analyze the exported data further.

Additionally, if you have access to an Android device where the notifications are not (reliably) received, you can use the on-device FCM diagnostics page:

To open the diagnostics page and check delivery status:

  1. From the Google Dialer, dial ##426## to open the FCM Diagnostics page. Or use the adb command: adb shell am start -n com.google.android.gms/.gcm.GcmDiagnostics
  2. Tap the EVENTS button to show the event log.
  3. Check whether the message was delivered to the device. There should be a log entry reading Received (id= …).

本文标签: nodejsHow to View Firebase Cloud Messaging Notifications Sent via Cloud FunctionsStack Overflow