2017-04-03 146 views
-1

我嘗試在Firebase雲消息傳遞中創建一組設備,並且我收到了一個ioexception「https://android.googleapis.com/gcm/googlenotification」。 我有幾個關於它的問題:Firebase雲消息傳遞 - 一組設備

  1. 我需要放在字段中:senderId,registrationId,idToken?
  2. 我如何更改這部分代碼來創建一個組,而不是添加到組?
  3. 我需要把「授權」,「鑰匙= AIzaS ...」嗎?

代碼:

public String addNotificationKey(
     String senderId, String userEmail, String registrationId, String idToken) 
    throws IOException, JSONException { 
    URL url = new URL("https://android.googleapis.com/gcm/googlenotification"); 
    HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
    con.setDoOutput(true); 

    // HTTP request header 
    con.setRequestProperty("project_id", senderId); 
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("Accept", "application/json"); 
    con.setRequestMethod("POST"); 
    con.connect(); 

    // HTTP request 
    JSONObject data = new JSONObject(); 
    data.put("operation", "add"); 
    data.put("notification_key_name", userEmail); 
    data.put("registration_ids", new JSONArray(Arrays.asList(registrationId))); 
    data.put("id_token", idToken); 

    OutputStream os = con.getOutputStream(); 
    os.write(data.toString().getBytes("UTF-8")); 
    os.close(); 

    // Read the response into a string 
    InputStream is = con.getInputStream(); 
    String responseString = new Scanner(is, "UTF-8").useDelimiter("\\A").next(); 
    is.close(); 

    // Parse the JSON string and return the notification key 
    JSONObject response = new JSONObject(responseString); 
    return response.getString("notification_key"); 

} 

回答

1

對於#3:

con.setRequestProperty("Authorization", "key=AIzaS..."); 
0
  1. 我需要把字段:senderId,registrationId,idToken?

請參閱其定義Credentials

發件人ID可在您的Firebase Console中找到。轉到項目設置,然後雲消息傳遞選項卡。

註冊令牌在客戶端應用程序端生成。根據客戶端應用類型here查看相應的安裝文檔。

idToken是(AFAIK)僅用於客戶端應用程序端。請參閱(Android)文檔here

  1. 如何更改這部分代碼以創建組而不添加到組?

變化

data.put("operation", "add"); 

data.put("operation", "create"); 
  • 哪裏需要把 「授權」,「鍵= AIzaS .. 。「?
  • 請參閱Puf's answer

    +0

    id令牌與註冊令牌有什麼區別? –

    相關問題