简体   繁体   English

Firebase Cloud Messaging使用Java发送HTTP-Post

[英]Firebase Cloud Messaging Send HTTP-Post in Java

I'm trying to create a Java Application which should send a Protocol to the Firebase Cloud Messaging Servers. 我正在尝试创建一个Java应用程序 ,它应该将协议发送到Firebase云消息服务器。 Now I have the problem, that I don't know how to see if my message was sent correctly . 现在我遇到了问题,我不知道如何查看我的消息是否正确发送 When I head to the Web-Console of Firebase/Notifications there is no message to see. 当我前往Firebase / NotificationsWeb控制台时没有消息可以看到。

My Code (just in case): 我的代码(以防万一):

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONObject;

public class Mainclass {
public static void main(String[] args) {
    // Declaration of Message Parameters
    String message_url = new String("https://fcm.googleapis.com/fcm/send");
    String message_sender_id = new String("XXXX-XXXX");
    String message_key = new String("key=XXXX-XXXX");

    // Generating a JSONObject for the content of the message
    JSONObject message = new JSONObject();
    message.put("message", "TEXT");
    JSONObject protocol = new JSONObject();
    protocol.put("to", message_sender_id);
    protocol.put("data", message);

    // Send Protocol
    try {
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost request = new HttpPost(message_url);
        request.addHeader("content-type", "application/json");
        request.addHeader("Authorization", message_key);

        StringEntity params = new StringEntity(protocol.toString());
        request.setEntity(params);
        System.out.println(params);

        HttpResponse response = httpClient.execute(request);
        System.out.println(response.toString());
    } catch (Exception e) {
    }
}
}

Output: 输出:

[Content-Type: text/plain; charset=ISO-8859-1,Content-Length: 59,Chunked: false]


HttpResponseProxy{HTTP/1.1 200 OK [Content-Type: application/json; charset=UTF-8, Date: Mon, 26 Dec 2016 12:09:13 GMT, Expires: Mon, 26 Dec 2016 12:09:13 GMT, Cache-Control: private, max-age=0, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: quic=":443"; ma=2592000; v="35,34", Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@32cf48b7}

Thank you! 谢谢!

PS There is a reference of FCM https://firebase.google.com/docs/cloud-messaging/server PS FCM https://firebase.google.com/docs/cloud-messaging/server提供了参考资料

By " how to see if my message was sent correctly ", I'm presuming you mean if it was sent towards the client app device. 通过“ 如何查看我的消息是否正确发送 ”,我假设您的意思是它是否已发送到客户端应用设备。

For this scenario, you can make use of the Delivery Receipts . 对于此方案,您可以使用交货收据 However, you'll have to implement an XMPP server , instead of the regular HTTP connection server. 但是,您必须实现XMPP服务器 ,而不是常规HTTP连接服务器。

One way to also have check is to make use of the Diagnostics tool , but a requirement for this to be available is for your app to be at least in Alpha Testing. 检查的一种方法是使用诊断工具 ,但要求可用的是您的应用程序至少在Alpha测试中。


The only logs/details you can see in the Firebase Console are the messages sent using Firebase Console itself, it won't show any messages sent using the FCM REST API. 您可以在Firebase控制台中看到的唯一日志/详细信息是使用Firebase控制台本身发送的消息,它不会显示使用FCM REST API发送的任何消息。


Else, if you are pertaining to just verify if the message was sent correctly towards the FCM servers, then you should check the response . 另外,如果您只是验证消息是否已正确发送到FCM服务器,那么您应该检查响应 Normally, it should return "success": 1 and a message_id . 通常,它应该返回"success": 1message_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM