如何在不使用 Firebase 控制台的情况下发送推送通知 [英] How to send push notifications without using Firebase console

查看:28
本文介绍了如何在不使用 Firebase 控制台的情况下发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 Firebase 具有消息传递功能的应用.它在应用程序中工作,我有一个 firebase 观察者设置来实时获取新消息.我不知道该怎么做是在那个观察者下触发推送通知.

我已经在我的应用程序中实现了 Firebase 云消息传递,以便我可以向我的控制台发送通知,但是我无法找到适用于 iOS 的资源,或者不仅限于使用控制台发送单个资源消息.

我知道这是一个广泛的问题,它可能会被标记为离题",但如果我能找到更多相关资源,我将不胜感激!

解决方案

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

<块引用>

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1内容类型:应用程序/json授权:承载 ya29.ElqKBGN2Ri_Uz...HnS_uNreA{信息":{令牌":bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...",通知" : {"body" : "这是一条 FCM 通知消息!","title" : "FCM 消息",}}}

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

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

admin.messaging().send({信息":{令牌":bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...",通知" : {"body" : "这是一条 FCM 通知消息!","title" : "FCM 消息",}}})

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.

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!

解决方案

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

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.

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

这篇关于如何在不使用 Firebase 控制台的情况下发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆