Android推送通知,fcm

时间:2017-06-28 05:52:58

标签: java firebase google-cloud-messaging firebase-cloud-messaging

我正在开发java rest 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"服务器API密钥"和userDeviceIdKey我在android studio中运行此代码得到的id

String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
                Settings.Secure.ANDROID_ID);

也许我没有清楚地理解smth,不管我做错了吗? 回复错误

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)

2 个答案:

答案 0 :(得分:3)

您可以使用Pushraven通过fcm向Android设备发送推送通知。

https://github.com/Raudius/Pushraven

添加jar并执行:

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文件,请转到提供的链接。 jar实现了发送fcm推送通知所需的一切。

答案 1 :(得分:1)

目前,您只能通过创建Firebase项目生成有效的服务器密钥,然后转到项目设置>云消息传递选项卡。通过Google Dev Console生成的新服务器API密钥预计不适用于GCM / FCM,如果确实如此,行为无法保证 - 您可能会收到401错误。

{{1}}的值应该通过调用{{1}}在客户端(通常)生成。请参阅Registration Token