在Firebase推送通知中发送图像
我希望能够在Firebase发送的推送通知上显示一个迷你图像。我遇到的问题是我使用一个有效负载,有时图像中的参数可以为null(未发送图像时),我正在阅读本文:在通知有效载荷中发送图像,
但始终在我的控制台上出现错误:
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};
});
});
问题是有时是图像是图像字段将为null(用户仅发送文本时),并且不应发送任何图像。
关于如何解决行为的任何想法。
亲切的问候
I want to be able to display a mini image on the Push Notifications sent from Firebase. The problem that I'm having is that I use a single payload and sometimes the parameter in Image can be null (when no image is sent), I was reading this article: Send an image in the notification payload
But always got an error on my Console like this:
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};
});
});
The problem is that sometimes the image field would be null (when the user only sends text) and it should not send any images.
Any ideas on how to fix the behavior.
Kind Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以验证image.isnotnull()检查服务是否使用图像响应。然后,如果不是零,则可以显示图像。
You could validate image.isNotNull() to check if the service respond with an image or not. Then, if it is not null, you can show the image.