<tfoot id='hQyff'></tfoot>

<legend id='hQyff'><style id='hQyff'><dir id='hQyff'><q id='hQyff'></q></dir></style></legend>

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

    • <bdo id='hQyff'></bdo><ul id='hQyff'></ul>

    1. <i id='hQyff'><tr id='hQyff'><dt id='hQyff'><q id='hQyff'><span id='hQyff'><b id='hQyff'><form id='hQyff'><ins id='hQyff'></ins><ul id='hQyff'></ul><sub id='hQyff'></sub></form><legend id='hQyff'></legend><bdo id='hQyff'><pre id='hQyff'><center id='hQyff'></center></pre></bdo></b><th id='hQyff'></th></span></q></dt></tr></i><div id='hQyff'><tfoot id='hQyff'></tfoot><dl id='hQyff'><fieldset id='hQyff'></fieldset></dl></div>
    2. 使用 Java 将推送通知从服务器发送到 android 设备

      Send push notification from server to android device in Java(使用 Java 将推送通知从服务器发送到 android 设备)
      1. <small id='F3299'></small><noframes id='F3299'>

          <tfoot id='F3299'></tfoot>
            <tbody id='F3299'></tbody>
            <bdo id='F3299'></bdo><ul id='F3299'></ul>

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

              • 本文介绍了使用 Java 将推送通知从服务器发送到 android 设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在为新的 FCM 苦苦挣扎...我以前使用过 FCM,现在我正在尝试 FCM...

                I am struggling with the new FCM... I used FCM before, now I am trying FCM...

                我正在尝试将我的应用服务器的推送通知发送到 android 设备.

                I am trying to send push notification for my app server to android device.

                我想用标准的Java包,尽量不要用Vert.x,apache的httpClient等……

                I wanna use the standard Java package, try not to use others such as Vert.x, apache's httpClient etc...

                这是我的代码:

                public void sendNotification(String messageBody)
                {
                    try
                    {
                        URL url = new URL("https://fcm.googleapis.com/fcm/send");
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setDoOutput(true);
                        conn.setDoInput(true);
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Content-Type", "application/json");
                
                        String apiKey = "AI...wE";
                
                        String credentials = "key=" + apiKey;
                        //String basicAuth = "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes());
                
                        String basicAuth = "Basic " + new String(Base64.encodeBase64(credentials.getBytes()));
                
                        conn.setRequestProperty ("Authorization", basicAuth);
                
                        String notfnStr = "{"body": "this is my body", "title": "this is my title"}";
                        String dataStr = "{"key1": "value1", "key2": "value2"}";
                
                        String bodyStr = "{"priority": "high", "to": "dFC8GW0N1Q8:APA91bHePPmC7QVV16LGnR6rqxwreHSv1GgawijZ_dZL9T70ZkiXIV8TW_ymAWkvFfXRiWJmtR_UGBXBv2iV2UhS8M-Tndw8sf8ZW6zIqfaiiVJao3G5HFbhqgA18ukNNtW_J7JaWkz8", " +
                                ""notification": " + notfnStr + ", "data": " + dataStr + "}";
                
                        System.out.println("### input: " + bodyStr);
                
                        OutputStream os = conn.getOutputStream();
                        os.write(bodyStr.getBytes());
                        os.flush();
                
                        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
                        }
                
                        BufferedReader br = new BufferedReader(new InputStreamReader(
                                (conn.getInputStream())));
                
                        String output;
                        System.out.println("Output from Server .... 
                ");
                        while ((output = br.readLine()) != null) {
                            System.out.println(output);
                        }
                
                        conn.disconnect();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                

                我得到的响应码是401,意思是未经授权...我猜是凭证格式不对...

                the response code i got is 401, which means unauthorized... i guess the format of credential is wrong...

                json 字符串是有效的 json 格式,所以我不用费心使用 JSONObject.

                The json string is in valid json format, so i don't bother to use JSONObject.

                对于字符串凭据,我尝试过 "key:" + apiKey;但仍然得到相同的结果.

                For the string credentials, i have tried "key:" + apiKey; but still got the same result.

                apiKey 字符串是从我从 Google 的 Firebase 控制台下载的 google-services.json 复制的.

                The apiKey String is copied from the google-services.json I downloaded from Firebase console in google.

                谷歌没有给出一个很好的例子......只是给了我这个:https://firebase.google.com/docs/cloud-messaging/downstream

                google didn't give a good example... just gave me this: https://firebase.google.com/docs/cloud-messaging/downstream

                如果有人知道怎么做,请回复.谢谢!!

                If any one knows how to do it, please reply. Thanks!!

                推荐答案

                在 JAVA 中使用 FCM 发送通知的完整示例

                public class FCMNotification {
                
                    // Method to send Notifications from server to client end.
                    public final static String AUTH_KEY_FCM = "API_KEY_HERE";
                    public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
                
                    public static void pushFCMNotification(String DeviceIdKey) throws Exception {
                
                        String authKey = AUTH_KEY_FCM; // You FCM AUTH key
                        String FMCurl = API_URL_FCM;
                
                        URL url = new URL(FMCurl);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                
                        conn.setUseCaches(false);
                        conn.setDoInput(true);
                        conn.setDoOutput(true);
                
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Authorization", "key=" + authKey);
                        conn.setRequestProperty("Content-Type", "application/json");
                
                        JSONObject data = new JSONObject();
                        data.put("to", DeviceIdKey.trim());
                        JSONObject info = new JSONObject();
                        info.put("title", "FCM Notificatoin Title"); // Notification title
                        info.put("text", "Hello First Test notification"); // Notification body
                        data.put("notification", info);
                
                        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                        wr.write(data.toString());
                        wr.flush();
                        wr.close();
                
                        int responseCode = conn.getResponseCode();
                        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();
                
                    }
                
                    @SuppressWarnings("static-access")
                    public static void main(String[] args) throws Exception {
                        FCMNotification.pushFCMNotification("USER_DEVICE_TOKEN");
                    }
                }
                

                这篇关于使用 Java 将推送通知从服务器发送到 android 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                Security - Array is stored directly(安全性 - 数组直接存储)
                SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)
                    <bdo id='7XySd'></bdo><ul id='7XySd'></ul>
                  • <small id='7XySd'></small><noframes id='7XySd'>

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

                        <tbody id='7XySd'></tbody>
                      <tfoot id='7XySd'></tfoot>