我有我的簡單Android應用程序使用REST Web服務。現在我想使用GCM從REST Web服務向Android應用程序發送通知。如何使用GCM從Java服務器向Android應用程序發送通知?
這是怎麼回事?有這個需求的簡單教程嗎?我搜索並找到了Google API,但我不明白這一點。
我有我的簡單Android應用程序使用REST Web服務。現在我想使用GCM從REST Web服務向Android應用程序發送通知。如何使用GCM從Java服務器向Android應用程序發送通知?
這是怎麼回事?有這個需求的簡單教程嗎?我搜索並找到了Google API,但我不明白這一點。
我創建了一個基於Java的測試服務器,作爲Maven插件來實現,對於GCMUtils項目: 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
也許這可以幫助您開始嗎?
關注這個URL https://firebase.google.com/docs/cloud-messaging/send-message
FCM URL
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());
}
這就是它!
這是使用從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();
}
}
好運