【问题标题】:FCM push notification - image not pushFCM 推送通知 - 图片不推送
【发布时间】:2020-04-21 19:29:01
【问题描述】:

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

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


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

}

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

我想如下显示我的通知。

我将不胜感激。去年年初试过,这个payload很好。

【问题讨论】:

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


【解决方案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 文件 node genereatekey.js,它将打印 OAuth2 令牌。

【讨论】:

  • 感谢您的帮助 - 我想知道这是一个问题。我阅读了手册,但我遇到了问题,因为我不知道如何生成不记名 auth2.0 密钥。 (非常复杂)
  • 我更新了我的答案,看看如何生成 OAuth2 令牌。
  • 陈妙昂感谢您的帮助。告诉我一个问题。我必须为每个用户生成新的 OAuth2 令牌吗?只生成一次就够了吗?
  • 只生成一个,但过期后需要重新生成。
  • @ChanMyaeAung 需要多长时间才能过期?
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 2020-09-17
  • 1970-01-01
  • 2022-08-22
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多