Java 服务器 -- 使用 POST 发送 Push 到 google Firebase Cloud

标签 java angularjs firebase http-post firebase-cloud-messaging

在我测试了推送通知是否可以与 Postman 配合使用后,我想在我的应用程序中发送消息时向 FCM 发送推送请求。调用的函数将访问我的 Java 服务器并调用如下函数:

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response sendMsg(Message m) throws ExceptionFacade {
...
}

所以每次我调用这个函数时,我都会向https://fcm.googleapis.com/fcm/send发送一个POST请求。带有 json。

我想知道是否已经有适用于 java 服务器的代码?还有一些如何实现它的帮助。

另外,我不明白是否可以使用 php 文件来完成此操作(我发现类似 https://github.com/Paragraph1/php-fcm 的东西)。我正在使用 angularjs。

谢谢大家!

最佳答案

这是运行良好的最终代码! 它发送一个像这样的 json :

{
 "to" : "...",
 "priority" : "high",
 "notification" : {
                   "title" : "hello",
                   "body" : "me"
  }
}

//不要忘记添加 common-codec 和 common-login jar 以确保构建成功。

public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws JSONException, IOException {

                HttpClient client = HttpClientBuilder.create().build();
                HttpPost post = new HttpPost("https://fcm.googleapis.com/fcm/send");
                post.setHeader("Content-type", "application/json");
                post.setHeader("Authorization", "key=FCM-API-KEY");
                JSONObject message = new JSONObject();
                message.put("to", "TOKEN-FCM-OF-THE-DEVICE");
                message.put("priority", "high");
                JSONObject notification = new JSONObject();
                notification.put("title", "Me");
                notification.put("body", "New message");
                message.put("notification", notification);
                post.setEntity(new StringEntity(message.toString(), "UTF-8"));
                HttpResponse response = client.execute(post);

                System.out.println(response);
                System.out.println(message);
    }

关于Java 服务器 -- 使用 POST 发送 Push 到 google Firebase Cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39159704/

相关文章:

java - DLL 中的内存泄漏检测

javascript - 使用名称初始化 ng-app 会禁用变量初始化

angularjs - 有什么方法可以在 android studio 中构建混合应用程序(Ionic、AngularJs、Cordova)

android - 当onTap时,所有图标都会更改颜色,如何在单击时仅更改一个?

javafx - 如何在 javafx 中使用 firebase

java - 如何添加类以在 Eclipse 中构建路径

java - Codility 钉板

javascript - Angular 解析被拒绝,但页面仍在加载

node.js - 如何从云功能移动 Firestore 文档?

java - JDialog不可见,组件可点击