Localize Push Notification

When we want to publish application to users around on the world make sure the application is localized. Otherwise, only some users can use our application. Normally, we can easily localize it by defining the languages with key-value that we want to support in the string files then get value by key to display on UI.

But how’s about with push notification? Server just send information to OS system and how we can handle localization for them.

There are several approaches in this case:

Approach 1: Server knows what language application is using. So it needs to have API to communicate between client and server. Then server will get localized strings to send notification to application.

Approach 2: Server only sends keys instead of using specific values.

In this article, I will choose the approach 2 because I think it takes less effort to implement. But anyway, it also has the disadvantage that if we want to change the value to display later, it means we have to rebuild and upload to store and it only takes time to app store review again.

In the following, we will learn about this approach…

First of all, you can create a simple iOS project by yourself and let register notification for application as the following:

Because I am using SwiftUI project so I will link AppDelegate to SwiftUI application. If you don’t use SwiftUI, you can omit:

Try to run application then the the permission should be displayed and you should allow this permission:

So far so good until now. But next time will be more excited!

Let define a payload.json file, we will this file use to send push notification to application.

As you see on the payload, we will see some kinds of keys:

title-loc-key: define key for title will display in localization
loc-key: define key for body message will display in localization
loc-args: define values for parameters in body message

There are alse some kind of other keys, you can prefer in apple document.

After defined keys in payload, now we also will link those keys to .strings file in application:

As you can see in the body message we have defined one parameter to map with value in loc-args:

"NOTIFICATION_BODY" = "Hello  %@, How are you today?";

That is all what we need. Let rebuild application and see how’s the result. One more to take note because Xcode 12 can supports to simulate notification on Simulator so we can easily push notification by command line:

xcrun simctl push booted <bundle_id> <path_to_payload>

So let try:

Thank you for your time to read this article. I hope that it will be helpful for you.

Senior Mobile Engineer