22

我有一个使用 REST Web 服务的简单 Android 应用程序。现在我想使用 GCM 从我的 REST Web 服务向 Android 应用程序发送通知。

这是怎么做的?这个要求有什么简单的教程吗?我搜索并找到了 Google API,但我不明白。

4

4 回答 4

5

我为 GCMUtils 项目创建了一个基于 Java 的测试服务器,作为 Maven 插件实现: https ://code.google.com/p/gcmutils/wiki/MavenPlugin#Test_server

这是源代码:https ://github.com/jarlehansen/gcmutils/tree/master/gcm-test-server

maven插件的来源:https ://github.com/jarlehansen/gcmutils/tree/master/gcmutils-maven-plugin

也许这可以帮助您入门?

于 2013-04-02T08:52:42.523 回答
1

这是一个用于将通知从 java 发送到应用程序 android 的功能。此代码使用 JSONObject,您必须将此 jar 添加到项目构建路径中。

注意:我使用 fcm

 import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;

import org.json.JSONObject;

public class FcmNotif {
  public final static String AUTH_KEY_FCM ="AIzB***********RFA";
  public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

// userDeviceIdKey is the device id you will query from your database

public void pushFCMNotification(String userDeviceIdKey, String title, String message) 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 json = new JSONObject();
json.put("to",userDeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", message); // Notification body
info.put("image", "https://lh6.googleusercontent.com/-sYITU_cFMVg/AAAAAAAAAAI/AAAAAAAAABM/JmQNdKRPSBg/photo.jpg");
info.put("type", "message");
json.put("data", info);
System.out.println(json.toString());

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
}

祝你好运

于 2017-05-21T15:00:08.740 回答
0

按照此网址 https://firebase.google.com/docs/cloud-messaging/send-message

FCM 网址

private String ANDROID_NOTIFICATION_URL = "https://fcm.googleapis.com/fcm/send"

通知键

private String ANDROID_NOTIFICATION_KEY = "Your key";

Java 代码

private void sendAndroidNotification(String deviceToken,String message,String title) throws IOException {
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject obj = new JSONObject();
        JSONObject msgObject = new JSONObject();
        msgObject.put("body", message);
        msgObject.put("title", title);
        msgObject.put("icon", ANDROID_NOTIFICATION_ICON);
        msgObject.put("color", ANDROID_NOTIFICATION_COLOR);

        obj.put("to", deviceToken);
        obj.put("notification",msgObject);

        RequestBody body = RequestBody.create(mediaType, obj.toString());
        Request request = new Request.Builder().url(ANDROID_NOTIFICATION_URL).post(body)
                .addHeader("content-type", CONTENT_TYPE)
                .addHeader("authorization", "key="+ANDROID_NOTIFICATION_KEY).build();

        Response response = client.newCall(request).execute();
        logger.debug("Notification response >>>" +response.body().string());
    }

而已 !!!

于 2016-09-21T11:45:28.853 回答
0
  1. 包 com.test;

    导入 java.io.BufferedReader;导入 java.io.IOException;导入 java.io.InputStreamReader;导入 java.io.OutputStream;导入 java.net.HttpURLConnection;导入 java.net.URL;

    公共类 Firebase {

    public static void main(String[] args) throws IOException {         final
    

    字符串 apiKey = "AAAAqf1B9uQ:**********_1MoeQBVPbVROXuyD4ERyV-i6nva0LAic9uRotN80C9utoaGL9Sp1GjrPr5-mtJBlxVNbuqG1zeg9LBnslZw-vyud3jW-11SWDfLBB26bcHuAi8bdnQCPcj3DWKX2h"; URL url = 新 URL(" https://fcm.googleapis.com/fcm/send "); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); System.setProperty("javax.net.debug","all"); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("授权", "key=" + apiKey);

        conn.setDoOutput(true);
    
        String input = "{\r\n\"to\":
    

    \"fdaxKOmRcAI:APA91bEXILacYEjypsbusKXHV_TuEzt_vsqhI5OxH-******************-l2qGIORSiE0W5B2d74yjXAz60l\", \r\n\"通知\": {\r\n\"title\" : \" title \",\r\n\"body\" : \" 单个vijay的正文\"\r\n}\r\n}";

                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())); 字符串输入线;StringBuffer 响应 = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);         }       in.close();
    
        // print result         System.out.println(response.toString());
    
    }
    

    }

    1. 项目清单
于 2018-04-11T06:03:01.657 回答