Firebase Java Server向所有设备发送推送通知 [英] Firebase Java Server to send push notification to all devices

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

问题描述

我尝试通过新的Firebase服务向我的Android设备发送推送通知。
我注册并设置了一个应用程序,我也把所有需要接收通知的代码放在android应用程序中。
通过Firebase控制台,我可以向我的应用程序发送通知,并收到并显示通知。
现在我想写一个java独立的服务器,发送一个通知给所有设备。这是我现在的代码:
$ b $ pre $ final String apiKey =我在这里添加了我的密钥;
网址=新网址(https://fcm.googleapis.com/fcm/send);
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type,application / json);
conn.setRequestProperty(Authorization,key =+ apiKey);

conn.setDoOutput(true);

String input ={\notification \:{\title \:\Test \},\to \:\测试\};

OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();

int responseCode = conn.getResponseCode();
System.out.println(\\\
发送'POST'请求到URL:+ url);
System.out.println(Post parameters:+ input);
System.out.println(Response Code:+ responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer(); ((inputLine = in.readLine())!= null){
response.append(inputLine);


}
in.close();

//打印结果
System.out.println(response.toString());

这就是我从服务器回来的结果:

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

不幸的是, ,我得到一个代码400返回。
我看到我需要注册设备,发送设备ID到服务器并保存在那里,然后遍历服务器上的所有注册设备发送消息。
是不是有一个简单的方法,只是发送一个消息到所有设备,就像在控制台?

你的帮助真的很感激,因为我一直试图让这个工作整天=(

问候,
达斯汀

解决方案

我不相信这是可能的,相反,我建议将所有设备注册到同一主题,然后您可以一次向所有设备发送消息。这:

从服务器发送主题消息



https://firebase.google.com/docs/cloud-messaging/topic-messaging


I am trying to send a Push notification to my android device with the new Firebase service. I registered and setup an app, also I put all the code needed to receive notification in the android app. Via the Firebase Console I can send a notification to my app and it is received and shown. Now I want to write a java standalone server, to send a notification to ALL devices. This is my current code:

final String apiKey = "I added my key here";
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);

conn.setDoOutput(true);

String input = "{\"notification\" : {\"title\" : \"Test\"}, \"to\":\"test\"}";

OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + input);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());

And this is the result that I am getting back from their servers:

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

Unfortunately, simply removing the "to" tag doesn't work, I am getting a code 400 returned then. I read that I need to register the device, send the device id to the server and save it there and then loop over all registered devices on the server to send the message. Isn't there an easier way, to just send a message to all devices, just like in the console?

Your help is really appreciated, since I have been trying to get this to work all day long =(

Regards, Dustin

解决方案

I don't believe this is possible. Instead, what I would suggest is register all devices to the same topic and then you can message all the devices at once. Here is the help documentation on this:

Send Topic Messages from the Server

https://firebase.google.com/docs/cloud-messaging/topic-messaging

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

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