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

问题描述:

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

I have an app that has a messaging feature via Firebase. It works within the app and I have a firebase observer setup to grab new messages in realtime. What I don't know how to do is trigger push notifications under that observer as well.

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

I've implemented Firebase Cloud Messaging into my app so that I can send a notification to my console, but am having trouble finding a resource that is for iOS or that doesn't just end at using the console to send a single message.

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

I know this a broad question, and it might be labelled "Off-topic" but if I could be directed towards any more related resources, I'd greatly appreciate it!

有一个用于使用Firebase Cloud Messaging发送消息的API.建议您不要查看 FCM服务器文档.它基本上采用HTTP POST请求的形式,例如文档中的示例:

There is an API for sending messages with Firebase Cloud Messaging. Instead of repeating it here, I recommend you check out the FCM server documentation. It basically takes the form of a HTTP POST request, such as this 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服务器密钥的人可以向您应用的所有用户发送无限制的消息.

To send messages to devices you will need to specify the so-called FCM server key. As its name applies, this key should only be used in trusted environments, such as a server that you control, your development machine, or Cloud Functions for Firebase. The reason for this is that someone who has your FCM server key can send unlimited messages to all users of your app.

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

There is also a Firebase Admin SDK that makes it easier to call the FCM server API to send messages on platforms that it supports. For more on this option, see the FCM Admin SDK documentation. That turns the above into something like this (on Node.js):

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