OneSignal - Swift - Receiving push notification in different languages

I am using the sdk from OneSignal. Let's assume, I have following use case:

User A use my app in German language. He triggers an action within the app e. g. a like on an image of User B. User B gets a push notification about the like. But User B uses the app in English language. How can I achieve that?

I mean, I can send the notification message in a particular language but if User B decides to change the language of the mobile device to Turkish, then I have to be flexibel.

Is there any way, I can access the incoming push notification on the target device?

Currently, my code for sending the notification from the source device looks like this:

public func notifyAboutSubscription(userObject:User, receiverArray:[String]) {
       var receiverArray = removeChallengeCreatorTokenFromArray(receiverArray: receiverArray)
        notificationTypeService.clearReceiverListForNotificationType(completionHandler: { (clearedReceiverArray) in
            receiverArray = clearedReceiverArray
    
            let source = self.determineUserType(userObject: userObject)
            OneSignal.postNotification(["contents": ["en": source
                + NSLocalizedString(FOLLOW_MESSAGE, comment: "")], "include_player_ids": receiverArray])
        }, receiverList: receiverArray, notificationType: NotificationType.follow)
    }

I am thankful for any hints


Best regards,

Nazar Medeiros

Answered by Claude31 in 424274022

I don't know OneSignal. Maybe you should ask them the question directly ?


But let's try some hints.


Are the messages predefined or built by user ?


If predefined,

1. - you should store all the messages in an array or better a dictionary, for each language:

key is the message number

value is the message in a language

2. - create a dictionary of those dictionaries

key is language code "en", "fr", …

value is the dictionary created at step 1

3. send the message number

4. get the message content in the selected language in receiver

Accepted Answer

I don't know OneSignal. Maybe you should ask them the question directly ?


But let's try some hints.


Are the messages predefined or built by user ?


If predefined,

1. - you should store all the messages in an array or better a dictionary, for each language:

key is the message number

value is the message in a language

2. - create a dictionary of those dictionaries

key is language code "en", "fr", …

value is the dictionary created at step 1

3. send the message number

4. get the message content in the selected language in receiver

Hello Claude31,


thanks a lot! You were right. The key/hint for the problem was putting the codes for all language that should be supported.

I basically have modified my function like this now


  public func notifyAboutSubscription(userObject:User, receiverArray:[String]) {
       var receiverArray = removeChallengeCreatorTokenFromArray(receiverArray: receiverArray)
        notificationTypeService.clearReceiverListForNotificationType(completionHandler: { (clearedReceiverArray) in
            receiverArray = clearedReceiverArray
    
            let source = self.determineUserType(userObject: userObject)
            OneSignal.postNotification(["contents": ["en": source +
                FOLLOW_MESSAGE, "de": source + " folgt dir jetzt", "tr": source + " seni takip ediyor"], "include_player_ids": receiverArray])
        }, receiverList: receiverArray, notificationType: NotificationType.follow)
    }


Depending on what language is set on the target device, the correct message is shown. That saved my day. Have a nice weekend!

OneSignal - Swift - Receiving push notification in different languages
 
 
Q