如何在Firebase消息服务中发送标题和正文以外的其他数据?(Node.js,Android)
我正在开发一个Android应用程序,通过使用基于Node.js的函数,我向Android发送通知,在Android中,onMessageReceived()函数用于接收数据以显示通知。现在,我面临的问题是,我想将一些字符串类型的数据与Title和Body并行发送。我应该做什么改变? 这是我的Node.js代码如何在Firebase消息服务中发送标题和正文以外的其他数据?(Node.js,Android),android,node.js,firebase,firebase-cloud-messaging,Android,Node.js,Firebase,Firebase Cloud Messaging,我正在开发一个Android应用程序,通过使用基于Node.js的函数,我向Android发送通知,在Android中,onMessageReceived()函数用于接收数据以显示通知。现在,我面临的问题是,我想将一些字符串类型的数据与Title和Body并行发送。我应该做什么改变? 这是我的Node.js代码 'use-strict' const functions = require('firebase-functions'); const admin = require('firebase
'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());
}
}
虽然我知道nodejs(或一般的js),但我昨天通过在有效负载中传递
数据对象实现了这一点
因此,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!"
}
}
{
“致”:“,
“优先级”:“正常”,
“安卓频道id”:-99,
“数据”:{
“头衔”:“某个头衔”,
“身体”:“某个身体”,
“更多数据”——“更多数据”,
“更多数据”——“更多数据
}
}
但是,如果我在有效负载中同时发送数据
和通知
,则不会调用GCMServiceListener
,应用程序只会显示有效负载的通知
部分中的内容
通过添加数据
部分(从而使通知成为“静默”通知),您将负责拦截消息,并使用通知生成器显示消息。只需一次小编辑,数据
消息内容仅由onMessageReceived()严格处理
当且仅当它是有效负载中唯一的消息类型时。如果它仍然有通知
消息,通常的行为是当应用程序在后台时,Android系统将通过显示通知
内容来处理通知,并且可以从打开的活动的意图中检索数据
内容。有关更多详细信息,请参阅。干杯