0

I'm following this tutorial, so far I have notifications working for web browsers (tested in chrome)

But I don't know how to send the icon via payload, so far what I've done is send the body of the request as follows (I'm using firebase cloud functions):

{
    'message': {
     token,
     'notification': {
         title,
         body,
         icon // <-- added the icon              
         }
    }
}

if I try to add the icon in the message payload I get a bad request when I do the post to the google FCM URL.

it works without adding the icon property to the payload, obviously, that's the error, the question again is how to send the icon in the payload to work.

thanks

EDIT, I'm posting my post function:

async function notification(messageBody) {
    const api = 'https://fcm.googleapis.com/v1/projects/{projectID}/messages:send';
    const accessToken = await getAccessToken();
    const response = await fetch(api, {
        headers: {
            'Accept': 'application/json',
            'Content-type': 'application/json',
            'Authorization': `Bearer ${accessToken}`
        },
        method: 'POST',
        body: messageBody
    });
    return response;
}
pedrommuller
  • 15,741
  • 10
  • 76
  • 126

1 Answers1

7

Try:

{
  "message": {
    "token" : token,
    "notification": {
      "title": title,
      "body": body
    },
    "webpush": {
      "headers": {
        "Urgency": "high"
      },
      "notification": {
        "body": body,
        "requireInteraction": "true",
        "icon": icon
      }
    }
  }
}
James Poag
  • 2,320
  • 1
  • 13
  • 20
  • I'm getting this error, but thanks for the answer: { error: { code: 400, message: 'Invalid JSON payload received. Unknown name "webpush": Cannot find field.\nInvalid JSON payload received. Unknown name "notification": Cannot find field.', status: 'INVALID_ARGUMENT', details: [ [Object] ] } } – pedrommuller Aug 27 '18 at 21:39
  • thanks, I figured I was sending the payload outside message, this answer helped me too: https://stackoverflow.com/questions/48919616/invalid-json-payload-received-unknown-name-click-action – pedrommuller Aug 27 '18 at 21:50