0

我開發java rest api服務,我需要將推送通知發送到android設備。我真的不知道如何正確地做到這一點,我的代碼是Android推送通知,fcm

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其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) 
+0

你得到的迴應是什麼? –

+0

沒有錯誤,但我沒有收到我的手機通知此ID爲「String userDeviceIdKey」 –

回答

3

您可以使用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文件去提供的鏈接。 該jar實現了發送fcm推送通知所需的一切。

+0

謝謝,但什麼是client_key,在我看來它的64-x字符串,我從這個代碼獲得字符串android_id =設置。 Secure.getString(getApplicationContext()。getContentResolver(), Settings.Secure.ANDROID_ID); –

+0

是的,client_key是由firebase提供的Android標識。 –

+0

@АлмасАбдразак如果答案對您有幫助,請將其標記爲已接受。 –

1

目前,你只應該產生一個有效服務器密鑰通過創建一個火力地堡項目,從那裏,去項目設置>雲端通訊標籤。通過Google開發者控制檯生成的新服務器API密鑰預計不適用於GCM/FCM,如果確實如此,行爲是不保證 - 您可能會收到401錯誤。

userDeviceIdKey的值應該在客戶端(通常是)通過調用getToken()生成。見Registration Token