How to resolve: language content complaining about OneSignal in Swift

I use OneSignal to manage my push notifications. For some notifications, I get:

Notifications must have content in English

But I just sent everything to English ...

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

Who, subtitles, contents are strings, the result is the identifier of the recipient. Most notifications are sent, for some I get an error message.

Console:

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

My full 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 much appreciated.

+5
source share
2 answers

I assume that one of the 4 fields is erroneous. Incorrect in this case may mean that, in a different encoding than English, it generated illegal characters or characters. Print each box before sending a notification.

Another situation that causes this error can be caused by the fact that one of the fields is empty or equal to zero. Print them again in the journal before posting the notice.

+3
source

Your JSON structure is incorrect. This is an example of 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

+1
source

Source: https://habr.com/ru/post/1260295/


All Articles