是否可以使用此数据结构将FCM发送到设备?创建文档时会触发该函数,但会出现错误“令牌必须为非空字符串或数组”。我的代码可以固定吗,还是我无法做到?
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const db = admin.firestore();
const fcm = admin.messaging();
export const sendToDevice = functions.firestore
.document('messages/{userid}')
.onCreate(async snapshot => {
const querySnapshot = await db
.collection('messages')
.doc('userid')
.collection('tokens')
.get();
const token = querySnapshot.docs.map(token => token.id);
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'Emergency',
body: `Alert`,
// icon: 'your-icon-url',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(token, payload);
});
云功能是否可以使用具有此类数据库结构的on create方法将FCM发送到设备?还是文档必须仅包含令牌而没有其他内容?
答案 0 :(得分:0)
我能够修改您的代码以在设备上获取消息。有关更多详细信息,请参见代码中的评论:
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'Emergency',
body: `Alert`,
// icon: 'your-icon-url',
// click_action: 'FLUTTER_NOTIFICATION_CLICK' // ** do not put it here
},
data: { click_action: 'FLUTTER_NOTIFICATION_CLICK', message: 'Your MSG' } // Use this data section
};