Android:Firebase 云功能推送消息未重定向到 onMessageReceived(FirebaseMessagingService) 方法

Android: Firebase Cloud function push message is not redirecting to onMessageReceived(FirebaseMessagingService) method

我是 Javascript 的新手,通过一些 Google 的帮助,我在 index.js 中编写了以下功能,以便在 Firestore table 时向我的应用程序发送推送消息会更新的

Index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotificationToFCMToken = functions.firestore.document('REQ/{mId}/Req/{bId}').onWrite(async (event) => {
//const uid = event.after.get('userUid');
const title = event.after.get('personName');
const content = event.after.get('content');
const fcmToken = event.after.get('token');
//let userDoc = await admin.firestore().doc(`users/${uid}`).get();
//let fcmToken = userDoc.get('token');

var message = {
    notification: {
        title: title,
        body: content,
    },
    token: fcmToken,
}

let response = await admin.messaging().send(message);
console.log(response);
});

以上功能正常运行,向应用程序发送推送消息并显示通知。但问题是它应该进入 FirebaseMessagingService class 中的应用程序,通过它我可以自定义它。

Firebase Cloud Messaging 可用于发送两种类型的消息。来自 messages types 上的文档:

  1. Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
  2. Data messages, which are handled by the client app.

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

因此,如果您希望您的应用程序代码处理该消息,您应该发送一条数据消息

您需要按照文档中的说明发送数据消息 here

// your message object should look like this
var message = {
    data: {
        title: title,
        body: content,
    },
    token: fcmToken,
}

两种类型:

1.Notification 留言:

var message = {
    notification: 
    {
        title: 'This is title',
        body: 'This is body!' 
    },
    condition: condition
};

这一个指定预定义的 user-visible key-value 对通知负载。 FCM 代表客户端应用程序自动向 end-user 台设备显示消息。通知消息有一组预定义的 user-visible 键。

2.Data 留言:

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

这一个指定消息负载的自定义 key-value 对。客户端应用程序负责处理数据消息。数据消息只有自定义 key-value 对。(参见 有关详细信息,我已经使用了它)。

所以当你想在客户端处理它时,你将使用数据消息

link显示在back-end中发送通知,Node.js也包括在内。