Change Language of Alert in Banner of Push Notification

Change language of alert in banner of Push Notification

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$@.";

Push notification Localization are displayed in the sending device language, not in the receiving one. Swift

I got it you calling

PushNotifications.sendPushNotification(to: customerFcmToken, title: String(format: NSLocalizedString( "ORDER_DELETED_PUSH_TITLE", comment: ""), orderId), subtitle: String(format: NSLocalizedString( "ORDER_DELETED_PUSH_SUBTITLE", comment: ""), UserDetails.fullName!), body: String(format: NSLocalizedString("ORDER_DELETED_PUSH_BODY", comment: "") , customerName))

And you are doing the localization, instead of sending the keys for localization.

iOS Push notification- Handle/Edit message in the 'alert' tag before showing it in the notification in status bar

There's a different behaviour for push notifications when the app is in the foreground/background and when the app isn't running at all. When the app is running, whether in the foreground, you'll receive a call to didReceiveRemoteNotification: and you'll be responsible for handling the notification's display (the operating system won't do it for you).

When the app is in the background, you'll receive a call to didReceiveRemoteNotification: only if/when the user presses the notification banner/alert displayed to him/her by the operating system. If the user chooses not to interact with the notification, your app won't even know about it.

If the app isn't running at all, the behaviour will be similar to when the app is in the background, only you'll receive the push notification indication and data in the userInfo dictionary of application:didFinishLaunchingWithOptions:

So, if your question refers to cases in which the app is in the foreground, that shouldn't be a problem and you can do anything you want with the alert field before displaying it to the user. Otherwise, it's the operating system's responsibility to show the alert so that makes it a bit more complicated. Try using the tools available to you for localizing the alerts, as described here: Change language of alert in banner of Push Notification. Hopefully, it will help. Good luck.

How to handle iOS Push Notifications in Different language

This is an old question but it deserves a new answer.

Localized push notifications can be solved either server side or in the app.

To send localized notifications server side requires that the server knows the users language preference and like aronspring suggested, a good time to do that is when registering the push token.

To do it locally on the phone you need to supply all possible notifications and their translations in the app bundle Localizable.strings file. The server would then instead of sending the text to be displayed, send the key to the corresponding translation in Localizable.strings.

Example aps payload:

"aps" : {
"alert" : {
"loc-key" : "SOME_MESSAGE"
}
}

For a better explanation read more here: Local and Remote Notification Programming Guide

iOS Push Notifications Localization

The docs imply that it's either/or: you should either set the body or the loc-key / loc-args properties.

Apple docs.

They might fall back to body if loc-key is not found, or they might never even look it up in Localizable.strings when body is present. Since docs don't define it, I would not count on the behavior one way or the other.

Flutter - How to format title and body of notification in background?

I could not get myBackgroundMessageHandler function to call when in background. I did find a couple of ways to localize notifications using loc_key like tags in iOS like this and in android like this.

However, this method was not suitable for my case because I had to send a bulk text (for example for sending promotions) in the notification. So I ended up saving the device's language in the backend when user subscribes something. That way backend can send the notification only in that language to that device.

How to Localize Push Notification Permission Dialog alert?

The only post I have found is
Customizing the iOS permission dialog for push notifications
Unfortunately, it is not possible to change the text / localisation of the message.

Handle Push Notification before it is displayed in the Notifications Banner

You can create "silent" push notifications. (See Apple documentation) When you receive such a silent notification:

  • parse the JSON
  • decide if you want to show it or not
  • if yes, just show a normal local notification, it looks same as a push-notification for the user

You will find plenty of SO tutorials on how to create local notifications.

You have to implement application:didReceiveRemoteNotification: fetchCompletionHandler.

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.



Related Topics



Leave a reply



Submit