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

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

问题描述

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

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



下面是解析出来的JSON在你眼中更容易:

  {
通知:{
title:note-Title,
body:note-Body
}
}

返回的回复只有两个字符:

就是这样,单词to。 (Headers报告400)我怀疑这与我的JSON中没有to有关。甚至会给一个to什么呢?我没有定义主题,设备也没有注册。但是,他们仍然能够接收来自Firebase管理控制台的通知。



由于Firebase的惊人限制,我想尝试一个仅限数据的JSON包通知处理,如果您的应用程序位于前台,通知将由您的处理程序处理,但是如果您的应用程序处于后台处理中,则会由Firebase服务进行INTERNALLY处理,并且不会传递给您的通知处理程序。显然,如果你通过API提交你的通知请求,但是只有在你使用数据的时候才可以解决这个问题。 (然后打破了使用相同的消息处理iOS和Android的能力。)在我的JSON中用data替换notification没有任何效果。



好的,然后我尝试在这里解决方案: Firebase Java服务器发送推送通知到所有设备
这似乎在我说:好的,即使通知可以通过管理控制台给每个人...这是不可能通过API。解决方法是让每个客户订阅一个主题,然后将通知推送到该主题。因此,首先在onCreate的代码:

  FirebaseMessaging.getInstance()。subscribeToTopic(allDevices); 

然后我发送新的JSON:

<$
通知:{
title:note-Title,
body:note-Body
},
to:allDevices
}

我至少从服务器得到一个真正的响应。 JSON响应:

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

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



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



底线:有没有人知道用什么JSON发送消息到所有运行应用程序的设备来镜像管理控制台功能?$ b $ Firebase通知没有API发送消息。 幸运的是,它基于Firebase云消息传递,它正好具有这样一个API。



通过Firebase通知和云消息传递,您可以将所谓的下游消息发送到设备有三种方式:


  1. 指定设备,如果您知道设备ID


  2. 设备组 .com / docs / cloud-messaging / topic-messagingrel =noreferrer>转到主题,这些只是设备可以订阅的关键字

您会注意到,没有办法显式发送到所有设备。您可以使用这些功能来构建这些功能。



要发送到主题,您的命令中有语法错误。主题以 / topics / 开头。由于在代码中没有这些,服务器将 allDevices 解释为设备ID。由于这是设备注册令牌的无效格式,因此会引发错误。



从发送消息到主题的文档:

  https://fcm.googleapis.com/fcm/send 
Content-Type:application / json
授权:key = AIzaSyZ-1u ... 0GBYzPu7Udno5aA

{
to:/ topics / foo-bar,
data:{
message: Firebase Cloud Messaging主题消息!,
}
}


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

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:

to

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.

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.

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");

then the new JSON I send:

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

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

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.

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.

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?

解决方案

Firebase Notifications doesn't have an API to send messages. Luckily it is built on top of Firebase Cloud Messaging, which has precisely such an API.

With Firebase Notifications and Cloud Messaging, you can send so-called downstream messages to devices in three ways:

  1. to specific devices, if you know their device IDs
  2. to groups of devices, if you know the registration IDs of the groups
  3. to topics, which are just keys that devices can subscribe to

You'll note that there is no way to send to all devices explicitly. You can build such functionality with each of these though.

For sending to a topic you have a syntax error in your command. Topics are identified by starting with /topics/. Since you don't have that in your code, the server interprets allDevices as a device id. Since it is an invalid format for a device registration token, it raises an error.

From the documentation on sending messages to topics:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "to": "/topics/foo-bar",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

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

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