错误:没有有效的注册ID iOS FCM

时间:2018-04-24 14:26:28

标签: swift firebase push-notification google-cloud-messaging firebase-cloud-messaging

所以我正在处理推送通知,我跟着这个tutorial,我能够成功地将我的firebase项目的推送通知发送到我的手机。现在,我正在尝试向设备组发送消息,如Google tutorial所示。

现在,我正在使用此方法获取设备令牌:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
    let   tokenString = deviceToken.reduce("", {$0 + String(format: "%02X",    $1)})
    print("deviceToken: \(tokenString)")
}

我试图将它添加到设备组(在服务器端),如下所示:

public static String addDeviceToGroup(String userKeyName, List<String> deviceToken, String notificationKey) throws IOException, JSONException {

    String result = "";
    URL url = new URL(API_URL_FCM_GROUP);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("project_id", SENDER_ID);

    JSONObject json = new JSONObject();
    json.put("registration_ids",deviceToken);
    json.put("operation","add");
    json.put("notification_key_name",userKeyName.trim());
    json.put("notification_key",notificationKey.trim());

    try {
        OutputStreamWriter wr = new OutputStreamWriter(
                conn.getOutputStream());
        wr.write(json.toString());
        wr.flush();

        int responseCode = conn.getResponseCode();
        System.out.print("RESPONSE CODE in addDeviceToGroup IS: " + responseCode  + "\n");
        BufferedReader br = null;
        if (responseCode == 200)
            br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        else
            br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        result = SUCCESS;
    } catch (Exception e) {
        e.printStackTrace();
        result = FAILURE;
    }
    System.out.println("FCM Notification added device to group successfully RESULT IS " + result);

    return result;
}

我一直得到400作为响应代码,我不断收到此错误:

{"error":"no valid registration ids"}

尝试从deviceGroup中删除时,我也遇到了同样的错误。

编辑:但是,当我得到令牌时:

let secondToken = InstanceID.instanceID().token()!

令牌已成功添加到设备组,当我将用户msg发送到属于该设备的通知密钥时,在我的控制台中显示它已交付:

{"success":2,"failure":0}

但它没有弹出我的设备。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我不知道你是否还需要这方面的帮助,但为了摆脱错误,你必须把devicetoken放在一个数组中。

  let postparameters = ["operation": "create",
                          "notification_key_name": name,
                          "registration_ids": [devicetoken]] as [String : Any]