2017-04-03 149 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令牌与注册令牌有什么区别? –

    相关问题