8

I'm using OneSignal to manage my push notifications. For some notifications, I'm receiving:

Notifications must have English language content

But I'm only sending everything in the english language...

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

Who, subtitle, contents are Strings, result is the receiver ID. Most of the notifications are sent, for some I receive the error message.

Console:

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

My complete function:

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)
            }
        })
    })
}

What am I missing? Help is very appreciated.

David Seek
  • 16,783
  • 19
  • 105
  • 136

2 Answers2

6

I assume that one of the 4 fields is wrong. Wrong in this case can mean that it has sone illegal characters or characters in a non english coding. Print each field before posting the notification.

Another situation which raise this error may be caused because one of the fields is empty or nil. Again print them to a log before posting the notification.

Vincent
  • 4,342
  • 1
  • 38
  • 37
  • 1
    looks like you have been right. i had a function with optional values, sending empty strings for example as subtitle body... a more concrete error message would have been helpful... thank you for your help – David Seek Nov 27 '16 at 21:30
  • 3
    i will. and i will also award you the bounty. there are just 6 days left of the bounty and maybe someone has useful tips or something like that. don't worry. will not forget bout you. – David Seek Nov 27 '16 at 21:42
3

Your JSON structure is wrong. This is a example of a code that works:

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

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

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

Allan Scofield
  • 1,884
  • 18
  • 14