Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change language of alert in banner of Push Notification

Tags:

I am facing problem to change the language of alert in banner when push comes. Actually i am working on an app which works in two language. One is English and second is Norwegian. The push I am receiving from my web server end and what the string it has in alert key is displayed in banner when push comes and you are outside of the app. But as a requirement we want that if I change the language from setting from English to Norwegian then when push comes its banner's alert string would also change to Norwegian. Will it be possible at my end or i have to change it from server whenever i change language?

like image 672
iEinstein Avatar asked Sep 04 '13 09:09

iEinstein


People also ask

Can push notifications be personalized?

Clearly, personalized push notifications can change the way you do business online. But according to a study, 70% of personalization experiences on an eCommerce website happen only when the user has logged into the account. But ideally, you'd want to offer a personalized experience even when they're not logged in.

How do I customize push notifications in Swift?

Go to xcode select File > New > Target > Notification Content Extension > Next > Product Name( name it as CustomUI) > Finish > Active scheme popUp(if comes then select cancel). Every Custom UI must have its unique category Identifier.


1 Answers

There are two ways to display localized text in a push notification in iOS:

Localize the message in your server

In this case, you must send the device language to your server. The code you need to add to your iOS app would be similar to the following:

NSString *preferredLanguage = [[NSLocale preferredLanguages] objectAtIndex:0]; const char *langStr = [preferredLanguage UTF8String]; [self sendCurrentLanguage:langStr]; // Method that communicates with your server 

Then you can send the notification message in the appropriate language by using the alert key in the notification JSON payload.

Send a localization string with the notification payload

You can send the localized string in the payload. The alert key accepts a child loc-key key that you can use to send a localized string:

"alert" : {      "loc-key" : "My Localized String",     ... } 

And then, in your Localizable.strings file inside the correspondent language identifier, add the following:

"My Localized String" = "The localized string in the language you want."; 

If you need to pass arguments to build the final localized string, you can pass it as a loc-args JSON array in the notification payload, too:

"alert" : {          "loc-key" : "My Localized String",         "loc-args" : [ "First argument", "Second argument" ],         ...     } 

And, in your Localizable.strings:

 "My Localized String" = "The localized string with first argument %@, and second argument %@." 

Or, if you need to change the positions:

 "My Localized String" = "The localized string with second argument %2$@, and first argument %1$@."; 
like image 138
Daniel Martín Avatar answered Dec 13 '22 14:12

Daniel Martín