2016-12-28 104 views
0

所以,我看到這個問題使用Jersey。但仍然沒有弄清楚如何從我的服務器發送簡單的推送通知給主題訂閱的用戶。 我不明白我的請求中有什麼問題,因爲我正在嘗試基於their examples進行構建。Firebase發送服務器通知java

public class OverSLANotifier { 

    public static void main(String[] args) throws Exception, FirebaseException { 
     String rawData = "{\"to\": \"/topics/sla\",\"data\": {\"message\": \"This is test push notification!\",}}"; 
     String encodedData = URLEncoder.encode(rawData, "UTF-8"); 

     URL u = new URL("https://fcm.googleapis.com/fcm/send"); 
     HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 
     conn.setDoOutput(true); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Authorization", "key=AI<redacted>VGB6YwPWrinoz1YFBgdKv4Pgm8s"); 
     conn.setRequestProperty("Content-Type", "application/json"); 
     OutputStream os = conn.getOutputStream(); 
     os.write(encodedData.getBytes()); 
     System.out.println(""+ conn.getResponseCode() + "-->>"+conn.getResponseMessage()); 
    } 

我的客戶端是properly set,如果我從控制檯發送,可以接收。

的迴應總是:

401 - >>未經授權

回答

1

你JSON似乎無效:

{\"to\": \"/topics/sla\",\"data\": {\"message\": \"This is test push notification!\",}} 

VS

{\"to\": \"/topics/sla\",\"data\": {\"message\": \"This is test push notification!\"}} 

而且檢查文件考慮:

用於發送消息的發件人帳戶無法驗證。 可能原因是:

  • 授權報頭丟失或與HTTP請求無效的語法。
  • 作爲關鍵字發送的項目編號無效。
  • 密鑰有效,但禁用了FCM服務。
  • 源自服務器的請求未在服務器密鑰IP中列入白名單 。

Source

PS:它的文檔中也無效你會發現here;)