如何在 Firebase Messaging Service 中发送标题和正文以外的其他数据? (Node.js、Android)

问题描述 投票:0回答:2

我正在开发一个 Android 应用程序,通过使用基于 Node.js 的函数,我向 Android 发送通知,在 Android 中,onMessageReceived() 函数用于接收数据以显示通知。现在我面临的问题是我想与标题和正文并行发送一些字符串类型数据。我应该做出哪些改变? 这是我的 Node.js 代码

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

exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite((change,context)=> {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log("User ID:"+user_id+" | Notification ID:"+notification_id);
return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult =>{
    const from_user_id = queryResult.data().from;
    const from_message = queryResult.data().Message;
    const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
    const to_data = admin.firestore().collection("Users").doc(user_id).get();
    return Promise.all([from_data,to_data]).then(result =>{
        const from_name = result[0].data().Name;
        const to_name = result[1].data().Name;
        const token_id = result[1].data().Token_ID;
        const payload = {
            notification: {
                title: "Hey! "+from_name+" here",
                body: "Dear "+to_name+", "+from_message+", Will you help me?",
                icon: "default"
            }
        };
        return admin.messaging().sendToDevice(token_id,payload).then(result =>{
            return console.log("Notification Sent.");
        });
    });
});
});

这是我的 Android 代码:

public class FirebaseMsgService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String messageTitle = remoteMessage.getNotification().getTitle();
    String messageBody = remoteMessage.getNotification().getBody();
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.enough);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(messageTitle)
            .setSound(sound)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    int mNotificationID = (int) System.currentTimeMillis();
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(mNotificationID,mBuilder.build());
}
}
android node.js firebase firebase-cloud-messaging
2个回答
2
投票

虽然我了解 NodeJS(或一般的 JS),但我昨天通过在有效负载中传递一个

data
对象来实现这个工作。

因此,google 发出的 json 请求(我仍在使用 GCM,但我确信 FCM 会是相同的,或非常相似的有效负载)如下所示:

{
  "to": "<GCM/FCM token>",
  "priority": "normal",
  "android_channel_id": -99,
  "data": {
    "title": "Some title",
    "body": "Some body",
    "more_data_one": "Some more data",
    "more_data_two": "Some more data, again!"
  }
}

但是,如果我在有效负载中同时发送

data
notification
,则
GCMServiceListener
永远不会被调用,并且应用程序只会显示有效负载的
notification
部分中的内容。

通过添加

data
部分(从而使通知成为“静默”通知),您就可以负责拦截消息并使用通知构建器显示它。


0
投票

 admin.messaging().send({
    token: <TOKEN>,
    notification: {
    title:'title',
    body:'body',
  },
  data: {
name:'yaman',
num:9999
},
  android: {
    notification: {
      sound: "default"
  }
}}) ; 

// 通过使用 npm require('firebase-admin');

© www.soinside.com 2019 - 2024. All rights reserved.