简体   繁体   中英

send an Image with a cloud-message firebase notification not working

I have implemented the notification successfully, but when I pass the image it isn't working for some reason.

Here is my implementation:

notification(user.fcmToken, {
    notification: {
      title: title,
      body: message,
      imageURL: "https://.../avatar/avatar1.png",
    },
  });

and the notification function:

exports.notification = async (registrationToken, message = message_notification, notify_options = options ) => {
   try {
        const response = await admin.messaging().sendToDevice(registrationToken, message, notify_options);
        console.log('Notificatin response:', JSON.stringify(response));
        if(response.successCount > 0) {
            return {
                statusMessage: 'SUCCESS',
                message: 'Sent Successfully',
                statusCode: 200,
            }
        }
        if(response.failureCount > 0) {
            return {
                statusMessage: 'ERROR',
                message: 'Falure',
                statusCode: 400,
            }
        }
    } catch (error) {
        console.log("Error", error)
       return {
        statusMessage: 'ERROR',
        message: 'SERVER ERROR',
        statusCode: 400,
      }
    }
};

You need use the method send or sendAll instead of sendToDevice. send and sendAll only accept one token string by request

     const notificationTokens = ["token1","token2"]
     const params = notificationTokens.map(token => ({
          token: token,
          notification: {
            body: "My TestBody",
            title: "My Title",
            imageUrl: 'http:imageurl.jpg'
          },
          data: {} //optional
        }))
         const response = await admin
        .messaging()
        .sendAll(registrationToken, message, notify_options);

if you have only string token you can use send instead send all.

Read the documentation for more information: https://firebase.google.com/docs/cloud-messaging/send-message#example-notification-message-image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM