Android 推送通知,fcm
Android push notifications , fcm
我正在开发 java 休息 api 服务,我需要向 android 设备发送推送通知。我不太确定如何正确执行此操作,我的代码是
public class FcmNotif {
public final static String AUTH_KEY_FCM = "My key";
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.addProperty("to", userDeviceIdKey.trim());
JsonObject info = new JsonObject();
info.addProperty("title", title); // Notification title
info.addProperty("body", message); // Notification body
info.addProperty("image", "https://lh6.googleusercontent.com/-sYITU_cFMVg/AAAAAAAAAAI/AAAAAAAAABM/JmQNdKRPSBg/photo.jpg");
info.addProperty("type", "message");
json.add("data", info);
System.out.println(json.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
// System.out.println(url.getH);
}
public static void main(String[] args) throws Exception {
FcmNotif fcmN=new FcmNotif();
fcmN.pushFCMNotification("user id ", "myFirstMessage", "hello");
System.out.println("Done");
}
}
AUTH_KEY_FCM 我从 https://developers.google.com/mobile/add "Server API Key" 和 userDeviceIdKey 获得它的 ID,我从 运行 这个代码在 android studio
String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
可能是我没理解清楚,我哪里做错了?
响应错误
java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.gmoika.FcmNotif.makeRequest(FcmNotif.java:88)
at com.gmoika.FcmNotif.main(FcmNotif.java:111)
目前,您只能通过创建 Firebase 项目来生成有效的 服务器密钥 ,从那里转到项目设置 > 云消息传递选项卡。通过 Google 开发者控制台生成的新服务器 API 密钥预计不适用于 GCM/FCM,如果适用,则 无法保证 - - 您可能会收到 401 错误。
userDeviceIdKey
的值应该在客户端(通常)通过调用 getToken()
生成。参见 Registration Token。
您可以使用 Pushraven 通过 fcm 向 android 台设备发送推送通知。
https://github.com/Raudius/Pushraven
添加罐子并执行:
Pushraven.setKey(my_key);
Notification raven = new Notification();
raven.title("MyTitle")
.text("Hello World!")
.color("#ff0000")
.to(client_key);
Pushraven.push(raven);
raven.clear();
有关更多详细文档和 jar 文件,请转到提供的 link。
该 jar 实现了发送 fcm 推送通知所需的一切。
我正在开发 java 休息 api 服务,我需要向 android 设备发送推送通知。我不太确定如何正确执行此操作,我的代码是
public class FcmNotif {
public final static String AUTH_KEY_FCM = "My key";
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.addProperty("to", userDeviceIdKey.trim());
JsonObject info = new JsonObject();
info.addProperty("title", title); // Notification title
info.addProperty("body", message); // Notification body
info.addProperty("image", "https://lh6.googleusercontent.com/-sYITU_cFMVg/AAAAAAAAAAI/AAAAAAAAABM/JmQNdKRPSBg/photo.jpg");
info.addProperty("type", "message");
json.add("data", info);
System.out.println(json.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
// System.out.println(url.getH);
}
public static void main(String[] args) throws Exception {
FcmNotif fcmN=new FcmNotif();
fcmN.pushFCMNotification("user id ", "myFirstMessage", "hello");
System.out.println("Done");
}
}
AUTH_KEY_FCM 我从 https://developers.google.com/mobile/add "Server API Key" 和 userDeviceIdKey 获得它的 ID,我从 运行 这个代码在 android studio
String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
可能是我没理解清楚,我哪里做错了? 响应错误
java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.gmoika.FcmNotif.makeRequest(FcmNotif.java:88)
at com.gmoika.FcmNotif.main(FcmNotif.java:111)
目前,您只能通过创建 Firebase 项目来生成有效的 服务器密钥 ,从那里转到项目设置 > 云消息传递选项卡。通过 Google 开发者控制台生成的新服务器 API 密钥预计不适用于 GCM/FCM,如果适用,则 无法保证 - - 您可能会收到 401 错误。
userDeviceIdKey
的值应该在客户端(通常)通过调用 getToken()
生成。参见 Registration Token。
您可以使用 Pushraven 通过 fcm 向 android 台设备发送推送通知。
https://github.com/Raudius/Pushraven
添加罐子并执行:
Pushraven.setKey(my_key);
Notification raven = new Notification();
raven.title("MyTitle")
.text("Hello World!")
.color("#ff0000")
.to(client_key);
Pushraven.push(raven);
raven.clear();
有关更多详细文档和 jar 文件,请转到提供的 link。 该 jar 实现了发送 fcm 推送通知所需的一切。