Ich habe meine einfache Android-Anwendung, die den REST - Webdienst verwendet. Jetzt möchte ich eine Benachrichtigung von meinem REST - Webdienst über GCM an die Android-Anwendung senden.
Wie geht das Gibt es ein einfaches Tutorial für diese Anforderung? Ich habe nach Google API gesucht und diese gefunden, aber ich verstehe das nicht.
Ich habe einen Java-basierten Testserver erstellt, der als Maven-Plugin für das GCMUtils-Projekt implementiert ist: https://code.google.com/p/gcmutils/wiki/MavenPlugin#Test_server
Hier ist der Quellcode: https://github.com/jarlehansen/gcmutils/tree/master/gcm-test-server
Quelle für das Maven-Plugin: https://github.com/jarlehansen/gcmutils/tree/master/gcmutils-maven-plugin
Vielleicht kann Ihnen das helfen?
Mit dieser Funktion wird eine Benachrichtigung von Java an die App Android gesendet. In diesem Code wird JSONObject verwendet. Sie müssen diese JAR-Datei in den Projektpfad einfügen.
hinweis: ich verwende 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();
}
}
viel Glück
paket com.test;
java.io.BufferedReader importieren; Java.io.IOException importieren; import Java.io.InputStreamReader; Java.io.OutputStream importieren; import Java.net.HttpURLConnection; Java.net.URL importieren;
öffentliche Klasse Firebase {
public static void main(String[] args) throws IOException { final
Zeichenfolge abmelden abmelden abmelden abmelden abmelden abmelden abmelden abmelden abmelden URL url = neue 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 ("Authorization", "key =" + apiKey);
conn.setDoOutput(true);
String input = "{\r\n\"to\":
"" fdaxKOmRcAI: APA91bEXILacYEjypsbusKXHV_TuEzt_vsqhI5OxH - ******************* - L2qGIORSiE0W5B2d74yjXAz60l\",\": \" title\",\r\n\\" body\": \" \"\ 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 ())); String inputLine; StringBuffer Antwort = new StringBuffer ();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine); } in.close();
// print result System.out.println(response.toString());
}
}
Folgen Sie dieser URL https://firebase.google.com/docs/cloud-messaging/send-message
FCM-URL
private String Android_NOTIFICATION_URL = "https://fcm.googleapis.com/fcm/send"
Benachrichtigungsschlüssel
private String Android_NOTIFICATION_KEY = "Your key";
Java-Code
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());
}
Das ist es !!!