如何解决:在 Swift 中使用 OneSignal 投诉的语言内容

How to resolve: language content complaining with OneSignal in Swift

我正在使用 OneSignal 来管理我的推送通知。对于某些通知,我收到:

Notifications must have English language content

但我用英语发送所有内容...

oneSignal.postNotification(["headings" : ["en": "\(who)"],
                            "subtitle" : ["en": "\(subtitle)"],
                            "contents" : ["en": "\(contents)"],
                            "include_player_ids": [result]],

who, subtitle, contents 为字符串,result 为收件人ID。大多数通知已发送,有些通知我收到错误消息。

控制台:

> ERROR: Create notification failed
Error Domain=OneSignalError Code=400 "(null)" UserInfo={returned={
    errors =     (
        "  Notifications must have English language content"
    );
}}

我的完整功能:

func sendPush(_ receiverID: String, _ who: String, _ didWhat: String, _ message: String?) {

    var subtitle = ""
    var contents = ""
    if message != nil {
        contents = message!
    }

    switch didWhat {
    case "likePost":
        subtitle = "liked your post"
    case "commentPost":
        subtitle = "commented on your post"
    case "likeComment":
        subtitle = "liked your comment"
    case "message":
        subtitle = "sent you a message"
    case "friendsRequest":
        subtitle = "sent you a friend request"
    case "friendAccept":
        subtitle = "accepted your friend request"
    case "follow":
        subtitle = "just followed you"
    default:
        break
    }

    getOneSignalPlayerID(receiverID, completion: { result in

        oneSignal.postNotification(["headings" : ["en": "\(who)"],
                                    "subtitle" : ["en": "\(subtitle)"],
                                    "contents" : ["en": "\(contents)"],
                                    "include_player_ids": [result]],
        onSuccess: { (success) in
            if success != nil {
                print(success!)
            }
        }, onFailure: { (failure) in
            if failure != nil {
                print(failure!)
                crashlyticsLog("getOneSignalPlayerID", failure!.localizedDescription)
            }
        })
    })
}

我错过了什么?非常感谢帮助。

我假设这 4 个字段之一是错误的。在这种情况下错误可能意味着它有一些非法字符或非英语编码中的字符。在发布通知之前打印每个字段。

引发此错误的另一种情况可能是因为其中一个字段为空或为零。在发布通知之前再次将它们打印到日志中。

您的 JSON 结构有误。这是有效代码的示例:

let objNotif = ["contents": ["en" : message], "include_player_ids": [userID!]]
    //print(objNotif)

    OneSignal.postNotification(objNotif, onSuccess: { (jsonSuccess) in
      //Post OK
      }) { (error) in
      //Error
    }

OneSignal 文档: https://documentation.onesignal.com/docs/ios-native-sdk