如何在不使用Firebase控制台的情况下发送推送通知

时间:2018-08-19 19:54:42

标签: swift firebase push-notification firebase-cloud-messaging

我有一个具有通过Firebase进行消息传递功能的应用程序。它可以在应用程序中运行,并且我有一个Firebase观察器设置来实时捕获新消息。我不知道该怎么办,也是在该观察者下触发推送通知。

我已经在我的应用程序中实现了Firebase Cloud Messaging,以便可以将通知发送到控制台,但是在查找适用于iOS的资源或无法仅使用控制台发送单个资源的资源时遇到了麻烦消息。

我知道这是一个广泛的问题,它可能被标记为“题外话”,但是如果我可以直接浏览其他相关资源,我将不胜感激!

2 个答案:

答案 0 :(得分:1)

有一个用于使用Firebase Cloud Messaging发送消息的API。建议您不要签出FCM server documentation,而不必在这里重复。它基本上采用HTTP POST请求的形式,例如以下example from the docs

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{
  "message":{
    "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
      }
   }
}

设备发送消息,您需要指定所谓的FCM服务器密钥。顾名思义,此密钥仅应在受信任的环境中使用,例如您控制的服务器,开发计算机或Firebase的Cloud Functions。原因是拥有FCM服务器密钥的人可以向您应用的所有用户发送无限制的消息。

还有一个Firebase Admin SDK,可以更轻松地调用FCM服务器API在其支持的平台上发送消息。有关此选项的更多信息,请参见FCM Admin SDK documentation。上面的内容变成了这样的内容(在Node.js上):

admin.messaging().send({
  "message":{
    "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
      }
   }
})

答案 1 :(得分:0)

您可以使用Postman或类似的方式将其发送。 您必须对https://fcm.googleapis.com/fcm/send

进行POST请求

设置标题:

Content-Type: application/json

Authorization: key=<legacy_server_key or server_key>

它看起来必须像

  

Authorization:key = AAAAwrVC26k:APA91bHz_ZRBjyoyevnVi0oey8yO_om9av_-YeUq ........

通知主体:

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