我希望能够在 Firebase 发送的推送通知上显示迷你图像。我遇到的问题是我使用单个有效负载,有时图像中的参数可以为空(当没有发送图像时),我正在阅读这篇文章:在通知有效负载中发送图像
但我的控制台上总是出现这样的错误:
export const sendChatNotification = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate(async (snap, ctx) => {
//In Comment OLD Way to get info data for the notification
const token = snap.get('sendToDeviceToken'); //snap.data().sendToDeviceToken;
const sender = snap.get('fullSenderName'); //snap.data().fullSenderName;
const body = snap.get('message'); //snap.data().message;
//const platform = snap.get('sendToDevicePlatform');
var tokens = [];
tokens.push(token);
console.log('Tokens: ' + tokens);
var imageInfo = body.substring(0, 38);
var message;
var image;
if(imageInfo === 'https://firebasestorage.googleapis.com') {
message = 'Image Received';
image = body;
} else {
message = body;
image = null;
}
const payload = {
tokens: tokens,
notification: {
title: sender,
body: message,
},
android: {
notification: {
channel_id: "roofdeck_default",
title: sender,
body: message,
notification_priority: 'priority_max',
color: '#DDE9F7',
icon: '@mipmap/ic_launcher',
image_url: image,
},
}, // El Codigo Comentariado aqui es para mostrar Badge en 1 con nuevos chats.
apns: {
payload: {
aps: {
badge: 1,
},
fcm_options: {
image: image,
},
},
webpush: {
headers: {
image: image,
}
},
}, // Hasta Aqui el Codgio del Badge.
// android: {
// notification: {
// notificationCount: 0,
// channelId: 'roofdeck_default',
// }
// }
};
fcm.sendMulticast(payload).then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return {success: true};
}).catch((error) => {
console.log('Error on sent message:', error);
return {error: error.code};
});
});
问题是有时图像字段将为空(当用户仅发送文本时)并且不应该发送任何图像。
有关如何解决该行为的任何想法。
亲切的问候
您可以验证 image.isNotNull() 以检查服务是否以图像响应。然后,如果不为空,就可以显示图像了。