使用swift3中的OneSignal sendTags发送数据时出现语法问题

时间:2017-03-02 02:48:47

标签: ios iphone swift onesignal

我在这一行面临构建问题。这需要很多时间来建立。

   strJsonBody = "{"
        +   "\"app_id\": " + GUARD_APP_ID + ","
                                    +   "\"included_segments\": [\"All\"],"
        +   "\"include_player_ids\": [" + playerId + "],"
        +   "\"data\": {\"name\": \"" + user_name + "\", \"email\": \"" + user_email + "\", \"phone\": \"" + user_phone + "\", \"uniqueCode\": \"" + user_uniqueCode + "\", \"uid\": \"" + user_uid + "\", \"type\": \"SOS\"},"
        +   "\"headings\": {\"en\": \"Resident SOS\"},"
        +   "\"ios_group\": \"sos\","
        +   "\"ios_sound\": \"sos\","
        +   "\"contents\": {\"en\": \"" + user_name + " signalled SOS\"}"
        + "}";

有没有其他方法可以做到这一点,当我在sendtags中传递这个strJsonBody然后它给出错误“无法将类型'String'的值转换为预期的参数类型'[AnyHashable:Any]!'”。

   OneSignal.sendTags(strJsonBody as Any, onSuccess: { (result) in
        print("success!")
    }) { (error) in
        print("Error sending tags - \(error?.localizedDescription)")
    }

2 个答案:

答案 0 :(得分:1)

let payload = [
    "app_id": GUARD_APP_ID,
    "include_player_ids": [playerId],
    "data": [
        "name": user_name,
        "email": user_email,
        "phone": user_phone,
        "uniqueCode": user_uniqueCode,
        "uid": user_uid,
        "type": "SOS" ],
    "headings": [ "en": "Resident SOS" ],
    "ios_sound": "sos",
    "contents": ["en": "\(user_name) signalled SOS"],
]
OneSignal.postNotification(payload, onSuccess: { (result) in
    print("success!")
}) { (error) in
    print("Error posting notification - \(error?.localizedDescription)")
}

删除:

"included_segments": ["All"], // Removed for security reasons
"ios_group": "sos", // Not an option

sendTags用于标记用户以后进行细分。您需要使用postNotification实时发送通知。

答案 1 :(得分:0)

当API期望与JSON兼容的字典时,您正在传递JSON字符串。这应该编译:

let tags = [
    "app_id": GUARD_APP_ID,
    "included_segments": ["All"],
    "include_player_ids": [playerId],
    "data": [
        "name": user_name,
        "email": user_email,
        "phone": user_phone,
        "uniqueCode": user_uniqueCode,
        "uid": user_uid,
        "type": "SOS" ],
    "headings": [ "en": "Resident SOS" ],
    "ios_group": "sos",
    "ios_sound": "sos",
    "contents": ["en": "\(user_name) signalled SOS"],
]
OneSignal.sendTags(tags, onSuccess: { (result) in
    print("success!")
}) { (error) in
    print("Error sending tags - \(error?.localizedDescription)")
}

但是,它们看起来并不像您要发送的标签。 看起来 就像您尝试直接从您的应用发送推送通知一样。我不确定OneSignal API是否支持这一点。