更改推送通知横幅中的警报语言 [英] Change language of alert in banner of Push Notification

查看:22
本文介绍了更改推送通知横幅中的警报语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当推送到来时,我遇到了更改横幅中警报语言的问题.实际上,我正在开发一个可以使用两种语言的应用程序.一个是英语,第二个是挪威语.我从我的网络服务器端收到的推送以及它在 alert 键中的字符串在推送到来时显示在横幅中并且你在应用程序之外.但作为一项要求,我们希望如果我将语言从设置从英语更改为挪威语,那么当推送到来时,其横幅的警报字符串也将更改为挪威语.是否可以在我结束时或者我每次更改语言时都必须从服务器更改它?

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?

推荐答案

在 iOS 的推送通知中有两种显示本地化文本的方法:

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

在您的服务器中本地化消息

在这种情况下,您必须将设备语言发送到您的服务器.您需要添加到 iOS 应用的代码类似于以下内容:

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

然后,您可以使用通知 JSON 负载中的 alert 键以适当的语言发送通知消息.

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

发送带有通知负载的本地化字符串

您可以在负载中发送本地化的字符串.alert 键接受可用于发送本地化字符串的子 loc-key 键:

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",
    ...
}

然后,在相应语言标识符内的 Localizable.strings 文件中,添加以下内容:

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

如果您需要传递参数来构建最终的本地化字符串,您也可以在通知负载中将其作为 loc-args JSON 数组传递:

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" ],
        ...
    }

并且,在您的 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$@.";

这篇关于更改推送通知横幅中的警报语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆