在 swift3 中使用 OneSignal sendTags 发送数据时出现语法问题
Syntax issue while sending data using OneSignal sendTags in swift3
我在这一行中遇到构建问题。建起来很费时间。
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 时,它给出了错误 "Cannot convert value of type 'String' to expected argument type '[AnyHashable : Any]!'"。
OneSignal.sendTags(strJsonBody as Any, onSuccess: { (result) in
print("success!")
}) { (error) in
print("Error sending tags - \(error?.localizedDescription)")
}
您正在传递一个 JSON 字符串,而 API 需要一个 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 来实时发送通知。
我在这一行中遇到构建问题。建起来很费时间。
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 时,它给出了错误 "Cannot convert value of type 'String' to expected argument type '[AnyHashable : Any]!'"。
OneSignal.sendTags(strJsonBody as Any, onSuccess: { (result) in
print("success!")
}) { (error) in
print("Error sending tags - \(error?.localizedDescription)")
}
您正在传递一个 JSON 字符串,而 API 需要一个 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 来实时发送通知。