简体   繁体   English

Android推送通知

[英]Android push notifications , fcm

I'm develop java rest api service and i need to make push notifications to android devices. 我正在开发Java Rest API服务,我需要向Android设备发出推送通知。 I'm not really sure how to do it properly, my code is 我不太确定如何正确执行,我的代码是

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 i get from https://developers.google.com/mobile/add "Server API Key" and userDeviceIdKey its id that i get from running this code in android studio 我从https://developers.google.com/mobile/add “服务器API密钥”和userDeviceIdKey获得的AUTH_KEY_FCM ID,这是我在android studio中运行此代码获得的

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

Maybe i don't understand smth clearly, whan am i doing wrong? 也许我不太清楚,我在做错什么吗? Response error 响应错误

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)

You can use Pushraven to send push notifications to android devices via fcm. 您可以使用Pushraven通过fcm将推送通知发送到android设备。

https://github.com/Raudius/Pushraven https://github.com/Raudius/Pushraven

Add the jar and do : 添加罐子并执行以下操作:

Pushraven.setKey(my_key);

Notification raven = new Notification();
raven.title("MyTitle")
  .text("Hello World!")
  .color("#ff0000")
  .to(client_key);

Pushraven.push(raven);

raven.clear();

For more detail documentation and jar file go to the link provided. 有关更多详细文档和jar文件,请转到提供的链接。 The jar implements everything that needs to be done to send fcm push notifications. 该jar实现了发送fcm推送通知所需的所有操作。

Currently, you're only supposed to generate a valid Server Key by creating a Firebase Project, from there, go to Project Settings > Cloud Messaging tab. 当前,仅应通过创建Firebase项目来生成有效的服务器密钥 ,然后从那里转到“项目设置”>“云消息传递”选项卡。 New Server API keys generated through the Google Dev Console is expected to not work for GCM/FCM, if it does, the behavior is not guaranteed -- you may receive a 401 error. 通过Google Dev Console生成的新服务器API密钥预计不适用于GCM / FCM,如果可以,则不能保证其行为-您可能会收到401错误。

The value for userDeviceIdKey is supposed to be generated on the client side (usually) by calling getToken() . userDeviceIdKey的值应该通常在客户端通过调用getToken()来生成。 See Registration Token . 请参阅注册令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM