使用Firebase Messaging通过Postman发送推送消息

64

我正在尝试使用Postman来发送一条使用Firebase Cloud Messaging服务的推送通知。

这是一条可用的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 - 适当地设置标题

输入图片描述

2- 在正文中,我正在使用raw

{
    "to" : "<YOUR_DEVICE_ID_TOKEN>"
    , 

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

  }
执行时,我得到了401 - 未经授权的返回信息。
正确发送推送通知需要什么?

2
{ "multicast_id": 4790719999662534961, "success": 0, "failure": 1, "canonical_ids": 0, "results": [ { "error": "InvalidRegistration" } ] } 收到了这个错误 - Vinod Kumar Marupu
这篇文章会对你有所帮助:https://medium.com/android-school/test-fcm-notification-with-postman-f91ba08aacc3 - Alireza Noorali
这个答案可以帮助:https://dev59.com/qlUL5IYBdhLWcg3waXUr#62670409 - Ramis
您可以在本文中找到有关如何通过Firebase Messaging API使用Postman发送FCM推送通知的内容。链接:https://medium.com/@sachinkhard/send-push-notification-via-firebase-by-postman-3f459ea5d170 - Ramesh-X
14个回答

62

正确设置头部授权(Authorization)密钥的方式是:

key=<API_ACCESS_KEY>

而不只是

<API_ACCESS_KEY>

这是一个愚蠢的错误,但由于这对于某些人用Postman测试Firebase Messaging可能会有用,所以我将保留这个问题。


我看到你在curl中已经有了--header "Authorization: key=<API_ACCESS_KEY>,而在Postman中却使用了错误的头部。 - CoolMind

57

打开 Postman,点击 输入请求 URL 文本框,输入 Firebase URL

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

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

enter image description here

现在点击标题并添加两个 params Content-TypeAuthorization

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

请见下方截图:

enter image description here

现在点击 Body,然后选择 Raw 并添加以下对象值。
{
  "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应该是这个样子的。点击右上角的发送按钮。

enter image description here

祝你好运!


谢谢,但目前在HTML中显示错误:“<TITLE>提供了无效(遗留)服务器密钥或发件人未被授权执行请求。</TITLE>”。 - CoolMind
如果您使用来自Cloud Messaging设置的服务器密钥或传统服务器密钥(从“AAAA”或“Alza”开始),则此方法有效。 - CoolMind
这只发送给指定的令牌。我如何在Postman中将其发送给所有用户? - Neo
这个功能非常好用,能够即时发送通知。需要更改的是Title为标题! - clauub
这对我有效。谢谢。 - Sharath B Naik
这个方法有效。我不得不复制已弃用/遗留的密钥。我应该在哪里获取更新的密钥?附言:感谢你详细的答案和可复制的示例。 - undefined

36

通过POSTMAN发布FCM

Body - to是令牌ID(应通过实例令牌生成) 以原始二进制application/json格式编写正文 body tye

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

   "notification": {

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

头部信息:应该包含授权服务器密钥。

内容类型:application/json headerimage 在此发布成功消息后: success message image


1
怎样获取令牌?有API吗? - Asymptote
阅读Firebase Android开发者文档。 - praveen dewangan
2
我得到了成功=1,但是我没有在手机上收到通知。 - phpdroid
如何向所有安装了应用程序的用户发送消息,而不是发送到主题? - DevOma
我们如何使用REST API在Node.js中将相同的东西转换? - Ankit Mishra
显示剩余4条评论

25

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

要使用 FCM HTTP v1 API 在 Postman 上进行测试,您需要先获取一个短期的 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发送推送通知。我已将标记打印到我的日志中,然后将该标记带到我的Postman进行测试,但没有意识到它已经在调用FCM的计划调用中使用过。

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


我收到了这个响应,但是没有通知到安卓应用程序。有什么想法吗? - Shridhar
@Shridhar,我和你有同样的问题,你找到解决办法了吗? - rgantla

13

提交

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"
    }
}

3
这个方法允许将消息发送到指定的令牌。我们如何向所有用户发送消息? - Neo
"to": "/topics/All",并订阅该主题。 - Taimoor Khan

11

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

Authorization : **key=**abcdefghijklmnopr2qrst253uv124wxyz_9shg

输入图像描述


6

你看过这篇文章了吗:使用POSTMAN测试FCM通知!

使用HTTP协议和POSTMAN发送数据消息

  • 你需要从Firebase控制台中复制Legacy服务器密钥,路径为:项目设置 > 云消息传递。

注意:Firebase已经将我们的服务器密钥升级到新版本。您可以继续使用您的Legacy服务器密钥,但建议您升级到最新版本。

  • 选择POST请求。输入请求URL:https://fcm.googleapis.com/fcm/send

  • 添加头部Authorization:key=<legacy_server_key>Authorization: key=<server_key>Content-Type: application/json.

  • 现在选择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集合,可以直接在Postman中运行。

希望这能帮到您。


谢谢!至少,它显示了比“无效(旧版)服务器密钥…”更适合的错误。 - CoolMind
如果您使用来自Cloud Messaging设置的服务器密钥或传统服务器密钥(从“AAAA”或“Alza”开始),则此方法有效。 - CoolMind
通知和数据对象有什么区别?为什么要同时使用它们? - clauub

4

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

请求头信息:

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

正文:

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

7
你从哪里获得这个Bearer令牌?如果我使用服务器密钥,FCM会抛出401错误。 - Evgenii Vorobei
此令牌由Firebase Admin SDK生成。 - Sergey Neskoromny

3
如果您想向多台设备发送通知,则应在正文中使用“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"
    }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接