FCM推送通知 – 图像未推送

huangapple go评论73阅读模式
英文:

FCM push notification - image not push

问题

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

{
     "notification" : {
         "body" : "测试通知",
         "title": "测试通知",
         "image" : "https://www.fillmurray.com/640/360"
     },


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

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

我希望我的通知显示如下。

FCM推送通知 – 图像未推送

我将非常感谢您的帮助。我在去年年初尝试过,这个有效负载是有效的。

英文:

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.

FCM推送通知 – 图像未推送

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

答案1

得分: 1

这是因为您正在使用不支持图像负载的旧版HTTP API。尝试迁移到HTTP v1 API,您将能够发送图像负载。请参考以下链接。
迁移指南
在通知负载中发送图像

迁移到HTTP v1时,您将需要OAuth令牌,如果您不知道如何生成它,我将在这里提供逐步指南。

创建OAuth令牌,请按照以下步骤进行。
步骤1. 从Firebase控制台获取服务帐户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"); //更改为您下载的JSON文件的路径

// 定义所需的范围。
var scopes = [
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/firebase.messaging"
];

// 使用服务帐户进行JWT客户端身份验证。
var jwtClient = new google.auth.JWT(serviceAccount.client_email,
null,serviceAccount.private_key,scopes);

// 使用JWT客户端生成访问令牌。
jwtClient.authorize(function(error, tokens) {
if (error) {
  console.log("生成访问令牌时出错:", 
  error);
} else if (tokens.access_token === null) {
console.log("提供的服务帐户没有生成访问令牌的权限");
} else {
  var accessToken = tokens.access_token;
  console.log(accessToken);

  // 有关如何使用访问令牌发送经过身份验证的请求的详细信息,请参见下面的“使用访问令牌”部分。
}
});

从终端运行generatekey.js文件,使用以下命令
node generatekey.js,它将打印OAuth2令牌。

英文:

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.<br>
Step 1. Get serviceaccount json file from firebase console.<br>
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<br>
Generating token will require running some program code using node,python or java and here I'll be using node.<br>
Create generatekey.js file with below code, and change path of json file inside code.

var {google} = require(&quot;googleapis&quot;);

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

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

// 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(&quot;Error making request to generate access token:&quot;, 
  error);
} else if (tokens.access_token === null) {
console.log(&quot;Provided service account does not have permission to generate access tokens&quot;);
} else {
  var accessToken = tokens.access_token;
  console.log(accessToken);

  // See the &quot;Using the access token&quot; 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.

答案2

得分: 0

const message = {
"notification": {
"body": "测试通知",
"title": "测试通知",
"android": {
"notification": {
"imageUrl": 'https://www.fillmurray.com/640/360'
}
}
}
}

英文:

try

const message = {
         &quot;notification&quot; : {
            &quot;body&quot; : &quot;test Notification&quot;,
            &quot;title&quot;: &quot;test Notification&quot;,
         &quot;android&quot;: {
            &quot;notification&quot;: {
             imageUrl: &#39;https://www.fillmurray.com/640/360&#39;}
}

huangapple
  • 本文由 发表于 2020年1月3日 16:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定