使用 Firebase Messaging 通过 Postman 发送推送

Sending Push via Postman using Firebase Messaging

我正在尝试使用 Postman 通过 Firebase 云消息服务发送单个推送通知。

这是一个用于相同目的的有效 cURL 命令,我将其用作参考。

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Firebase\"} \"priority":\"10"}"

到目前为止我做了什么..

1 - 适当设置 Headers

2- 在 Body 处,我使用 raw

{
    "to" : "<YOUR_DEVICE_ID_TOKEN>"
    , 

    "notification": {
    "body": "Firebase Cloud Message"
  }

  }

执行时,我回来了401 - Unauthorized

正确发送推送通知还缺少什么?

在 Header 设置 Authorization 键的正确方法是

key=<API_ACCESS_KEY>

不仅如此

<API_ACCESS_KEY>

愚蠢的错误,但由于这可能对使用 Postman 测试 Firebase Messaging 的人有用,所以我将问题悬而未决。

查看下面的屏幕截图如何设置授权密钥

授权:**key=**abcdefghijklmnopr2qrst253uv124wxyz_9shg

通过 POSTMAN 发布 FCM

Body - to 是token id(应该通过instance token生成) 以原始二进制文件写入正文 application/json

{
   "to": "cpa8cZPjq-w:APA91bF122f1Rnhu9v47bL
   YMajaNTHAIU5SzItDwTy9o2MCIveG0PlK78VPvp3d
   CqjwnUKZ4
   ngi1trSyM3_aXttW62iknFfbPGtjRLhZr6wq-3qFdboz8gzdOGPz**********",

   "notification": {

    "body": "Hello",
    "title": "This is test message."
   }
   }

header:应该有授权:服务器密钥

内容类型:application/json 在此处发布成功消息后:

对于新的 FCM HTTP v1 API,通过 Postman 测试推送通知的方法已经改变,现有的解决方案仅解决了测试推送通知的遗留方法:https://firebase.google.com/docs/cloud-messaging/send-message

要使用 FCM HTTP v1 API 在 Postman 上进行测试,您需要先获取 short-lived Oauth 2 令牌。您可以根据 Firebase 网站上的信息生成一个:https://firebase.google.com/docs/cloud-messaging/auth-server

我使用 Python 使用下面的代码生成了我的 - 请记住 pip 安装包,以便您可以成功地将包导入到您的代码中:

from oauth2client.service_account import ServiceAccountCredentials

def _get_access_token():
  """Retrieve a valid access token that can be used to authorize requests.

  :return: Access token.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      'service-account.json', FCM_SCOPE)
  access_token_info = credentials.get_access_token()
  return access_token_info.access_token

获得令牌后 - 您可以在授权下将其插入您的 Postman:

重要提示:

就我而言,我在尝试测试计划任务时遇到了一些困难才能使其正常工作,该任务将每隔几分钟向 FCM 发送一次推送通知。 我已将令牌打印到我的日志中,然后将该令牌交给我的邮递员进行测试,但没有意识到它已经在计划的 FCM 调用中使用了。

在那种情况下,因为它已被使用,令牌将不再有效,我的 Postman 测试全部失败。在这种情况下,您需要为 Postman 测试生成新的令牌。

一定要包括 content-type : application/json header

如果您不包含 content-type header 语句,它会因误导性错误而失败: 错误=缺少注册

你检查过了吗:Test FCM Notification with POSTMAN!

通过 POSTMAN

使用 HTTP 协议发送数据消息
  • 您必须从 Firebase 控制台 > 项目设置 > 云消息传递复制旧版服务器密钥

Note: Firebase has upgraded our server keys to a new version. You may continue to use your Legacy server key, but it is recommended that you upgrade to the newest version.

  • SelectPOST。输入请求 URL 作为 https://fcm.googleapis.com/fcm/send

  • 添加Headers授权:key=<legacy_server_key>Authorization: key=<server_key>Content-Type: application/json.

  • 现在 Select Body > raw > JSON (application/json) 并添加以下代码:

    {
         "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
         "collapse_key" : "type_a",
         "notification" : {
             "body" : "Body of Your Notification",
             "title": "Title of Your Notification"
         },
         "data" : {
             "body" : "Body of Your Notification in Data",
             "title": "Title of Your Notification in Title",
             "key_1" : "Value for key_1",
             "key_2" : "Value for key_2"
         }
    }

我为你创造了POSTMAN Collection,Run in Postman directly.

希望对您有所帮助。

POST https://fcm.googleapis.com/v1/projects/projectID/messages:send

headers:

Authorization:Bearer ya29.c.ElrXBjdxUzOI................o7_Y2shX2Je4tEe0

body:

  {
      "message": {
        "token": "dlqj05MCnNI:APA91bGhCX6NhT.................QDpKhGheHyhhJ6T",
        "android": {
          "notification": {
            "title": "New Content!",
            "body": "A new video has been uploaded.",
            "icon": "your_icon"
          }
        }
      }
    }

打开 Postman,点击 Enter request URL 文本框,输入 firebase url

https://fcm.googleapis.com/fcm/send

然后将请求类型更改为 POST。请参见下面的屏幕截图:

现在单击页眉并添加两个 params Content-TypeAuthorization.

Content-Type= application/json
Authorization=AIzaSyAEb3NhsfGw1ih5mn8cmrGUAXMp86waQ //FCM SERVER KEY

见下图:

现在点击 Body 而不是 select Row 并添加值作为对象,如下所示

{ 
"to":"eB5papU2Xdc:APA91bFFvc3dXru1fN5JY8U19oHIpfGhPUx7Ll7v9vJYTsIGZ15mDwB2Wpep3flLK85IUqqs2WqJwjYHSDYX28oJ1wTP0R2TDc2ba_uVjUauDcp3pCNKr_0KlghOnS", 
 "notification" : {
 "body" : "New announcement assigned",
 "OrganizationId":"2",
 "content_available" : true,
 "priority" : "high",
 "subtitle":"Elementary School",
 "Title":"hello"
 },
 "data" : {
 "priority" : "high",
 "sound":"app_sound.wav",
 "content_available" : true,
 "bodyText" : "New Announcement assigned",
 "organization" :"Elementary school"
}
}

现在你的 Postman 看起来像这样。单击右上角的发送按钮。

祝你好运!

服务器密钥到底是什么?我使用了(在上面的 pitcure 上)服务器密钥和旧版服务器密钥,但它们都不起作用。

有人知道哪里出了问题吗?对于令牌 -> 我使用了从 firebase 消息传递中获得的 FCM 令牌,如下所示:

firestoreMessaging.getToken()

POST

https://fcm.googleapis.com/fcm/send

页眉

Content-Type : application/json 
Authorization : key=your_server_key

正文

{ 
    "to" : "your_token",
    "priority": "high",
    "notification": {
        "title": "Title",
        "body" : "First Notification",
        "text": "Text"
    }
}

发送 FCM throw Postman 的步骤:

  1. URL 发送: https://fcm.googleapis.com/v1/projects/projectid-34543/messages:send
  2. 请求类型:POST
  3. Headers: Content-Type -> application/json & 授权 -> 承载
  4. 在 body 部分输入 APS 负载 正确的设备令牌。
  5. 点击发送。

这里有更多信息:https://whosebug.com/a/62670409/1151916

İ如果要将通知发送到多个设备,请在正文中使用“registration_ids”而不是“to”;

 { 
    "registration_ids":[
"somethingTokens7234134143",
"somethingTokens3452343185"
], 

     "notification" : {
     "body" : "New announcement assigned",
     "content_available" : true,
     "priority" : "high",
     "subtitle":"Elementary School",
     "Title":"hello"
     },
     "data" : {
     "priority" : "high",
     "sound":"app_sound.wav",
     "content_available" : true,
     "bodyText" : "New Announcement assigned",
     "organization" :"Elementary school"
    }
    }