<tfoot id='oZytb'></tfoot>

      <bdo id='oZytb'></bdo><ul id='oZytb'></ul>

  1. <small id='oZytb'></small><noframes id='oZytb'>

    <i id='oZytb'><tr id='oZytb'><dt id='oZytb'><q id='oZytb'><span id='oZytb'><b id='oZytb'><form id='oZytb'><ins id='oZytb'></ins><ul id='oZytb'></ul><sub id='oZytb'></sub></form><legend id='oZytb'></legend><bdo id='oZytb'><pre id='oZytb'><center id='oZytb'></center></pre></bdo></b><th id='oZytb'></th></span></q></dt></tr></i><div id='oZytb'><tfoot id='oZytb'></tfoot><dl id='oZytb'><fieldset id='oZytb'></fieldset></dl></div>
      <legend id='oZytb'><style id='oZytb'><dir id='oZytb'><q id='oZytb'></q></dir></style></legend>

      Firebase 云消息传递 REST API Spring

      Firebase cloud messaging rest API spring(Firebase 云消息传递 REST API Spring)

      <i id='dPHrf'><tr id='dPHrf'><dt id='dPHrf'><q id='dPHrf'><span id='dPHrf'><b id='dPHrf'><form id='dPHrf'><ins id='dPHrf'></ins><ul id='dPHrf'></ul><sub id='dPHrf'></sub></form><legend id='dPHrf'></legend><bdo id='dPHrf'><pre id='dPHrf'><center id='dPHrf'></center></pre></bdo></b><th id='dPHrf'></th></span></q></dt></tr></i><div id='dPHrf'><tfoot id='dPHrf'></tfoot><dl id='dPHrf'><fieldset id='dPHrf'></fieldset></dl></div>

      <small id='dPHrf'></small><noframes id='dPHrf'>

        <tbody id='dPHrf'></tbody>
      <tfoot id='dPHrf'></tfoot>

          <legend id='dPHrf'><style id='dPHrf'><dir id='dPHrf'><q id='dPHrf'></q></dir></style></legend>
            • <bdo id='dPHrf'></bdo><ul id='dPHrf'></ul>
                本文介绍了Firebase 云消息传递 REST API Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我必须在 Spring Java 中为多层架构创建一个 Rest API,其中需要为 Firebase 云消息传递 (FCM) 构建 DAO、控制器、服务管理器以将推送通知消息发送到 android 应用程序,但我可以无法在 Java 中配置服务器以向设备发送通知.我怎么可能?

                I have to make a Rest API in Spring Java for a multi tier arch in which DAO, Controller, Service manager needs to be build for a Firebase Cloud Messaging (FCM) to send push notifications messages to android application, but I can not able configure a server in Java to send notifications to devices. How could I?

                推荐答案

                以下是实现此目的的方法:

                Here is the way that you can achieve this:

                第 1 步:在 firebase 上创建项目并生成服务器密钥.

                Step 1: Create project on firebase and generate server key.

                第 2 步:为 fcm 服务器生成一个 json 对象.这里的消息可能包含数据对象和通知对象.它还必须具有接收器 fcm id.示例 json 是这样的:

                Step 2: Generate a json object for fcm server. Here message may contains data object and notification object. It must also have receiver fcm id. Sample json is like:

                {
                    "notification":
                        {
                            "notificationType":"Test",
                        "title":"Title ",
                        "body":"Here is body"
                        },
                    "data":
                        {"notificationType":"Test",
                        "title":"Title ",
                        "body":"Here is body"
                        },
                        "to":"dlDQC5OPTbo:APA91bH8A6VuJ1Wl4TCOD1mKT0kcBr2bDZ-X8qdhpBfQNcXZWlFJuBMrQiKL3MGjdY6RbMNCw0NV1UmbU8eooe975vvRmqrvqJvliU54bsiT3pdvGIHypssf7r-4INt17db4KIqW0pbAkhSaIgl1eYjmzIOQxv2NwwwwXg"
                }
                

                第 3 步:编写一个 Rest 调用者服务,它将通过以下 url 与 fcm 服务器进行通信:

                Step 3 : Write a Rest caller service that will communicate with fcm server by following url:

                https://fcm.googleapis.com/fcm/send
                

                这是示例工作代码:

                public class PushNotificationServiceImpl {
                    private final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";
                    private final String FIREBASE_SERVER_KEY = "YOUR_SERVER_KEY";
                
                
                    public void sendPushNotification(List<String> keys, String messageTitle, String message) {
                
                
                        JSONObject msg = new JSONObject();
                
                        msg.put("title", messageTitle);
                        msg.put("body", message);
                        msg.put("notificationType", "Test");
                
                        keys.forEach(key -> {
                            System.out.println("
                Calling fcm Server >>>>>>>");
                            String response = callToFcmServer(msg, key);
                            System.out.println("Got response from fcm Server : " + response + "
                
                ");
                        });
                
                    }
                
                    private String callToFcmServer(JSONObject message, String receiverFcmKey) {
                        RestTemplate restTemplate = new RestTemplate();
                        HttpHeaders httpHeaders = new HttpHeaders();
                        httpHeaders.set("Authorization", "key=" + FIREBASE_SERVER_KEY);
                        httpHeaders.set("Content-Type", "application/json");
                
                        JSONObject json = new JSONObject();
                
                        json.put("data", message);
                        json.put("notification", message);
                        json.put("to", receiverFcmKey);
                
                        System.out.println("Sending :" + json.toString());
                
                        HttpEntity<String> httpEntity = new HttpEntity<>(json.toString(), httpHeaders);
                        return restTemplate.postForObject(FIREBASE_API_URL, httpEntity, String.class);
                    }
                }
                

                你只需要调用 sendPushNotification(ListreceiverKeys, String messageTitle, String message) 然后接收者就会收到推送消息

                You have to just call sendPushNotification(List<String> receiverKeys, String messageTitle, String message) then receiver will get push message

                谢谢:)

                这篇关于Firebase 云消息传递 REST API Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <tbody id='c6OYe'></tbody>

                    <i id='c6OYe'><tr id='c6OYe'><dt id='c6OYe'><q id='c6OYe'><span id='c6OYe'><b id='c6OYe'><form id='c6OYe'><ins id='c6OYe'></ins><ul id='c6OYe'></ul><sub id='c6OYe'></sub></form><legend id='c6OYe'></legend><bdo id='c6OYe'><pre id='c6OYe'><center id='c6OYe'></center></pre></bdo></b><th id='c6OYe'></th></span></q></dt></tr></i><div id='c6OYe'><tfoot id='c6OYe'></tfoot><dl id='c6OYe'><fieldset id='c6OYe'></fieldset></dl></div>
                  1. <tfoot id='c6OYe'></tfoot>
                  2. <legend id='c6OYe'><style id='c6OYe'><dir id='c6OYe'><q id='c6OYe'></q></dir></style></legend>

                    <small id='c6OYe'></small><noframes id='c6OYe'>

                        <bdo id='c6OYe'></bdo><ul id='c6OYe'></ul>