0

I have a problem, I am trying to send FCM notification by http. When I send below payload:

{
     "notification" : {
         "body" : "test Notification",
         "title": "test Notification",
         "image" : "https://www.fillmurray.com/640/360"
     },


"to":"firebasetoken",
"priority":"high"

}

I get notification on my mobile devices but the notification contains only Title and Body. I tried changing image to imageurl but that also didn't work.

I want display my notification as below.

enter image description here

I will be grateful for help. I tried at the beginning of last year and this payload was good.

3

2 Answers 2

1

This is because you are using legacy HTTP API which doesn't support image payload. Try migrating to HTTP v1 API and you'll be able to send image payload. Follow these links. Migration guide. Send image in notification payload

When migrating to HTTP v1, you'll need oAuth token and in case you don't know how to generate it, I'll provide step by step guide here.

To create oAuth token, follow these steps.
Step 1. Get serviceaccount json file from firebase console.
Go to firebase console -> project setting -> service account tab and click generate new private key to download json file. The json file contains some credential information.

Step 2. Generate token
Generating token will require running some program code using node,python or java and here I'll be using node.
Create generatekey.js file with below code, and change path of json file inside code.

var {google} = require("googleapis");

// Load the service account key JSON file.
var serviceAccount = 
require("path/to/downloaded.json"); //change path to your downloaded json file

// Define the required scopes.
var scopes = [
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/firebase.messaging"
 ];

// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(serviceAccount.client_email,
null,serviceAccount.private_key,scopes);

// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
if (error) {
  console.log("Error making request to generate access token:", 
  error);
} else if (tokens.access_token === null) {
console.log("Provided service account does not have permission to generate access tokens");
} else {
  var accessToken = tokens.access_token;
  console.log(accessToken);

  // See the "Using the access token" section below for information
  // on how to use the access token to send authenticated requests to
  // the Realtime Database REST API.
}
});

Run generatekey.js file from terminal with command node genereatekey.js and it will print OAuth2 token.

6
  • Thanks for help - i was wondering it is a couse that. I read manual but i have problem because I don't know how i can generate bearer auth2.0 key. (Its is very complicate)
    – kogutu
    Commented Jan 3, 2020 at 10:01
  • 1
    I updated my answer and see how to generate OAuth2 token. Commented Jan 4, 2020 at 3:43
  • Chan Myae Aung thanks for help. Tell me on one question. MustI generate always new OAuth2 token per user? Is it enough to generate it only once?
    – kogutu
    Commented Jan 4, 2020 at 10:22
  • Generate only one but you'll need to regenerate once it's expired. Commented Jan 4, 2020 at 16:30
  • @ChanMyaeAung how long does it take to expire?
    – fabiodrs
    Commented Jan 1, 2022 at 15:24
0

try

const message = {
         "notification" : {
            "body" : "test Notification",
            "title": "test Notification",
         "android": {
            "notification": {
             imageUrl: 'https://www.fillmurray.com/640/360'}
}
1
  • I tried imageURL but it is not working! Commented Jan 4, 2023 at 0:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.