如何将 channelId 添加到我的通知中?
How do I add a channelId to my notification?
我编写了一个云函数来向特定设备发送云消息。
export const onGroupInvitationCreate = functions.region('europe-west1').firestore.document('users/{userId}/userGroupInvitations/{invitationId}').onCreate(async (handler, context) => {
const invitation = GroupInvitationModel.fromFirestoreDocumentData(handler);
const documentList = await userCollection.doc(invitation.receiverId).collection('userDeviceData')
.listDocuments();
const futureDocuments = [];
for(const documentReference of documentList)
futureDocuments.push(documentReference.get());
const deviceDatas = (await Promise.all(futureDocuments)).map(deviceData => DeviceDataModel.fromFirestoreDocumentData(deviceData));
const futureNotifications = [];
for(const deviceData of deviceDatas)
{
const translator = deviceData.language !== 'nl' ? languages.en : languages.nl;
const payload: admin.messaging.MessagingPayload = {
// notification: {
// title: `${translator['invitationTitle' as keyof typeof translator]}`,
// body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
// clickAction: 'FLUTTER_NOTIFICATION_CLICK',
// },
data: {
title: `${translator['invitationTitle' as keyof typeof translator]}`,
body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
senderId: invitation.senderId,
senderDisplayName: invitation.senderDisplayName,
type: 'invitation',
sentAt: new Date().toISOString(),
}
}
futureNotifications.push(messaging.sendToDevice(deviceData.cloudMessagingToken, payload));
}
await Promise.all(futureNotifications).catch((error) => console.error(`There was an error while notifying receiver ${invitation.receiverId}`, error));
return Promise.resolve('Status 200');
});
我将如何提供 channelId?我正在使用
messaging.sendToDevice();
它需要一个注册令牌、一个有效负载和选项。我注意到其中 none 个具有 属性 channelId。
AndroidConfig
确实有一个 属性 channelId,但我在发送通知时找不到如何使用它。
这就是 sendToDevice 接受的参数
sendToDevice(
registrationToken: string | string[],
payload: admin.messaging.MessagingPayload,
options?: admin.messaging.MessagingOptions
): Promise<admin.messaging.MessagingDevicesResponse>;
您需要在 MessagingPayload
的 notification
部分传递 android_channel_id
属性。有关旧版 FCM APIs 接受的所有 Android 相关选项的完整列表,请参阅 https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support 中的 table 2b。
如果您想使用 AndroidConfig
,您必须先迁移到新的 FCM API(即 send()
方法)。
感谢@Hiranya Jayathilaka 我按照以下方式设置,他的回答是正确的。
const message: TokenMessage = {
token: deviceData.cloudMessagingToken,
data: {
title: `${translator['messageTitle' as keyof typeof translator]} ${group.displayName}`,
body: `${sender.displayName} ${translator[type as keyof typeof translator]}`,
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
senderId: sender.id,
senderDisplayName: sender.displayName,
groupId: group.id,
type: 'groupMessage',
messageType: type,
sentAt: new Date().toISOString(),
},
android: {
priority: 'high',
notification: {
channelId: '59054',
}
}
}
console.log(message);
console.log('notification sent');
futureNotifications.push(messaging.send(message));
const message = {
notification:{
title: 'title',
body:'message',
},
android: {
notification: {
channelId: 'Your notification channelID',
}
},
topic:'topic'
};
我编写了一个云函数来向特定设备发送云消息。
export const onGroupInvitationCreate = functions.region('europe-west1').firestore.document('users/{userId}/userGroupInvitations/{invitationId}').onCreate(async (handler, context) => {
const invitation = GroupInvitationModel.fromFirestoreDocumentData(handler);
const documentList = await userCollection.doc(invitation.receiverId).collection('userDeviceData')
.listDocuments();
const futureDocuments = [];
for(const documentReference of documentList)
futureDocuments.push(documentReference.get());
const deviceDatas = (await Promise.all(futureDocuments)).map(deviceData => DeviceDataModel.fromFirestoreDocumentData(deviceData));
const futureNotifications = [];
for(const deviceData of deviceDatas)
{
const translator = deviceData.language !== 'nl' ? languages.en : languages.nl;
const payload: admin.messaging.MessagingPayload = {
// notification: {
// title: `${translator['invitationTitle' as keyof typeof translator]}`,
// body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
// clickAction: 'FLUTTER_NOTIFICATION_CLICK',
// },
data: {
title: `${translator['invitationTitle' as keyof typeof translator]}`,
body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
senderId: invitation.senderId,
senderDisplayName: invitation.senderDisplayName,
type: 'invitation',
sentAt: new Date().toISOString(),
}
}
futureNotifications.push(messaging.sendToDevice(deviceData.cloudMessagingToken, payload));
}
await Promise.all(futureNotifications).catch((error) => console.error(`There was an error while notifying receiver ${invitation.receiverId}`, error));
return Promise.resolve('Status 200');
});
我将如何提供 channelId?我正在使用
messaging.sendToDevice();
它需要一个注册令牌、一个有效负载和选项。我注意到其中 none 个具有 属性 channelId。
AndroidConfig
确实有一个 属性 channelId,但我在发送通知时找不到如何使用它。
这就是 sendToDevice 接受的参数
sendToDevice(
registrationToken: string | string[],
payload: admin.messaging.MessagingPayload,
options?: admin.messaging.MessagingOptions
): Promise<admin.messaging.MessagingDevicesResponse>;
您需要在 MessagingPayload
的 notification
部分传递 android_channel_id
属性。有关旧版 FCM APIs 接受的所有 Android 相关选项的完整列表,请参阅 https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support 中的 table 2b。
如果您想使用 AndroidConfig
,您必须先迁移到新的 FCM API(即 send()
方法)。
感谢@Hiranya Jayathilaka 我按照以下方式设置,他的回答是正确的。
const message: TokenMessage = {
token: deviceData.cloudMessagingToken,
data: {
title: `${translator['messageTitle' as keyof typeof translator]} ${group.displayName}`,
body: `${sender.displayName} ${translator[type as keyof typeof translator]}`,
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
senderId: sender.id,
senderDisplayName: sender.displayName,
groupId: group.id,
type: 'groupMessage',
messageType: type,
sentAt: new Date().toISOString(),
},
android: {
priority: 'high',
notification: {
channelId: '59054',
}
}
}
console.log(message);
console.log('notification sent');
futureNotifications.push(messaging.send(message));
const message = {
notification:{
title: 'title',
body:'message',
},
android: {
notification: {
channelId: 'Your notification channelID',
}
},
topic:'topic'
};