如何通过 CURL 向所有设备发送 Firebase 通知? [英] How do you send a Firebase Notification to all devices via CURL?

查看:26
本文介绍了如何通过 CURL 向所有设备发送 Firebase 通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向所有应用用户(在 Android 上)发送通知,基本上是复制通过 Firebase 管理控制台发送通知时发生的情况.这是我开始的 CURL 命令:

I'm attempting to send out a notification to all app users (on Android), essentially duplicating what happens when a notification is sent via the Firebase admin console. Here is the CURL command I begin with:

curl --insecure --header "Authorization: key=AIzaSyBidmyauthkeyisfineL-6NcJxj-1JUvEM" --header "Content-Type:application/json" -d "{"notification":{"title":"note-Title","body":"note-Body"}}" https://fcm.googleapis.com/fcm/send

curl --insecure --header "Authorization: key=AIzaSyBidmyauthkeyisfineL-6NcJxj-1JUvEM" --header "Content-Type:application/json" -d "{"notification":{"title":"note-Title","body":"note-Body"}}" https://fcm.googleapis.com/fcm/send

下面是解析出来的 JSON,让您更容易理解:

Here's that JSON parsed out to be easier on your eyes:

{
"notification":{
    "title":"note-Title",
    "body":"note-Body"
    }
}

返回的响应只有两个字符:

The response that comes back is just two characters:

就是这样,到"这个词.(标题报告 400)我怀疑这与我的 JSON 中没有to"有关.人们甚至会为到"加上什么?我没有定义主题,设备也没有注册任何东西.但是,他们仍然可以从 Firebase 管理面板接收通知.

That's it, the word "to". (Headers report a 400) I suspect this has to do with not having a "to" in my JSON. What would one even put for a "to"? I have no topics defined, and the devices have not registered themselves for anything. Yet, they are still able to receive notifications from the Firebase admin panel.

由于 Firebase 通知处理的惊人限制,我想尝试一个仅数据"JSON 包,如果您的应用程序在前台,通知将由您的处理程序处理,但如果您的应用程序在后台,它由 Firebase 服务在内部处理,永远不会传递给您的通知处理程序.显然,如果您通过 API 提交通知请求,则可以解决此问题,但前提是您仅使用数据.(这会破坏使用相同消息处理 iOS 和 Android 的能力.)在我的任何 JSON 中将通知"替换为数据"都无效.

I'm want to attempt a "data only" JSON package due to the amazing limitation of Firebase notification processing whereby if your app is in the foreground, the notification gets processed by YOUR handler, but if your app is in the background, it gets processed INTERNALLY by the Firebase service and never passed to your notification handler. APPARENTLY this can be worked around if you submit your notification request via the API, but ONLY if you do it with data-only. (Which then breaks the ability to handle iOS and Android with the same message.) Replacing "notification" with "data" in any of my JSON has no effect.

好的,然后我在这里尝试了解决方案:Firebase Java Server 向所有设备发送推送通知在我看来,这似乎是在说好吧,尽管可以通过管理控制台向所有人发送通知……但通过 API 却不太可能."解决方法是让每个客户端订阅一个主题,然后将通知推送到该主题.首先是onCreate中的代码:

Ok, then I attempted the solution here: Firebase Java Server to send push notification to all devices which seems to me to say "Ok, even though notifications to everyone is possible via the Admin console... it's not really possible via the API." The workaround is to have each client subscribe to a topic, and then push out the notification to that topic. So first the code in onCreate:

FirebaseMessaging.getInstance().subscribeToTopic("allDevices");

然后是我发送的新 JSON:

then the new JSON I send:

{
"notification":{
    "title":"note-Title",
    "body":"note-Body"
    },
"to":"allDevices"
}

所以现在我至少收到了来自服务器的真实响应.JSON 响应:

So now I'm getting a real response from the server at least. JSON response:

{
"multicast_id":463numbersnumbers42000,
"success":0,
"failure":1,
"canonical_ids":0,
"results":
    [
    {
    "error":"InvalidRegistration"
    }
    ]
}

这带有一个 HTTP 代码 200.好吧...根据 https://firebase.google.com/docs/cloud-messaging/http-server-ref 带有InvalidRegistration"的 200 代码表示注册令牌有问题.也许?因为文档的那部分是针对消息传递服务器的.通知服务器是一样的吗?不清楚.我在其他地方看到该主题可能需要几个小时才能激活.这似乎会使创建新聊天室变得毫无用处,所以这似乎也不行.

And that comes with a HTTP code 200. Ok... according to https://firebase.google.com/docs/cloud-messaging/http-server-ref a 200 code with "InvalidRegistration" means a problem with the registration token. Maybe? Because that part of the documentation is for the messaging server. Is the notification server the same? Unclear. I see elsewhere that the topic might take hours before it's active. It seems like that would make it useless for creating new chat rooms, so that seems off as well.

当我可以从头开始编写一个应用程序,在我之前从未使用过 Firebase 的几个小时内收到通知时,我感到非常兴奋.在达到 Stripe.com 文档的水平之前,它似乎还有很长的路要走.

I was pretty excited when I could code up an app from scratch that got notifications in just a few hours when I had never used Firebase before. It seems like it has a long way to go before it reaches the level of, say, the Stripe.com documentation.

底线:有人知道要提供什么 JSON 来向所有运行该应用的设备发送消息以反映管理控制台功能吗?

Bottom line: does anyone know what JSON to supply to send a message out to all devices running the app to mirror the Admin console functionality?

推荐答案

似乎不再支持此方法(感谢@FernandoZamperin).请看看其他答案!

您可以使用 condition 键向不在组中的实例发送消息,而不是订阅主题.您的数据可能如下所示:

Instead of subscribing to a topic you could instead make use of the condition key and send messages to instances, that are not in a group. Your data might look something like this:

{
    "data": {
        "foo": "bar"
    },
    "condition": "!('anytopicyoudontwanttouse' in topics)"
}

请参阅 https://firebase.google.com/docs/cloud-消息/发送消息#send_messages_to_topics_2

这篇关于如何通过 CURL 向所有设备发送 Firebase 通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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