FCM推送通知-图片未推送

时间:2020-01-03 08:05:31

标签: android ionic-framework push-notification firebase-cloud-messaging ionic-native

我有问题,我正在尝试通过http发送FCM通知。当我发送以下有效载荷时:

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


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

}

我在移动设备上收到通知,但该通知仅包含标题和正文。我尝试将图像更改为imageurl,但这也没有用。

我要显示我的通知,如下所示。

enter image description here

我将非常感谢您的帮助。我在去年年初尝试过,这种有效载荷很好。

1 个答案:

答案 0 :(得分:1)

这是因为您使用的是不支持图像有效负载的旧式HTTP API。尝试迁移到HTTP v1 API,您将能够发送图像有效载荷。请点击以下链接。 Migration guideSend image in notification payload

当迁移到HTTP v1时,您将需要oAuth令牌,并且如果您不知道如何生成它,我将在此处提供分步指南。

要创建oAuth令牌,请按照以下步骤操作。
步骤1.从Firebase控制台获取serviceaccount json文件。
转到Firebase控制台->项目设置->服务帐户选项卡,然后单击生成新私钥以下载json文件。 json文件包含一些凭据信息。

第2步:生成令牌
生成令牌需要使用node,python或java运行一些程序代码,这里我将使用node。
使用以下代码创建generatekey.js文件,并在代码内部更改json文件的路径。

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.
}
});

使用命令从终端运行generatekey.js文件 节点genereatekey.js ,它将显示OAuth2令牌。