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

Ios 在swift3中使用OneSignal sendTags发送数据时出现语法问题,ios,iphone,swift,onesignal,Ios,Iphone,Swift,Onesignal,我在这方面面临构建问题。建造起来要花很多时间 strJsonBody = "{" + "\"app_id\": " + GUARD_APP_ID + "," + "\"included_segments\": [\"All\"]," + "\"include_player_ids\": [" + playerId + "]," + "\"data\": {

我在这方面面临构建问题。建造起来要花很多时间

   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]!'


当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是否支持这一点

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来实时发送通知。

是,我正在尝试从我的应用程序发送推送通知。您需要
postNotification
方法:Dave我正在尝试发送通知并获得成功响应,但我没有收到通知。请发布另一个问题。Dave请检查此问题
"included_segments": ["All"], // Removed for security reasons
"ios_group": "sos", // Not an option