【发布时间】:2019-11-09 12:21:23
【问题描述】:
我正在翻译我的应用程序,但我遇到了远程通知问题。 以下文档
在您的 App Bundle 中存储本地化内容
如果您对通知使用一致的消息集,您 可以在您的应用程序包中存储消息文本的本地化版本 并使用有效载荷中的 loc-key 和 loc-args 键来指定哪个 要显示的消息。 loc-key 和 loc-args 键定义消息 通知的内容。当存在时,本地系统搜索 应用程序的 Localizable.strings 文件,用于匹配与 锁定键中的值。然后它使用来自 字符串文件作为消息文本的基础,替换任何 具有由 loc-args 键指定的字符串的占位符值。 (您也可以使用 title-loc-key 和 title-loc-args 键。)
我有 NSLocalizedString 的标题、副标题和正文。问题是,当我从英语设置设备发送通知时,我会在意大利语设置设备上收到英语远程通知。我将所有Localized key:value 对存储在Localizable.string 文件中,以便我理解它可以工作,在收到通知时,应从Localizable.string 文件中获取当前设备语言的值并显示。我对所有警报小狗都这样做,它在那里完美地工作,但在远程通知上,它保持发送设备的语言。我将 FCM 用于远程通知,所以我可能会错过在有效负载中传递一些值。你能看到有效载荷中缺少什么吗?
非常感谢。
static func sendPushNotification(to receiverToken: String, title: String, subtitle: String, body: String) {
print("PushNotifications.sendPushNotification Started")
let serverKey = firebaseServerKey
let url = NSURL(string: "https://fcm.googleapis.com/fcm/send")
let postParams: [String : Any] = [
"to": receiverToken,
"mutable_content": true,
"content_available": true,
"priority": "high",
"notification": [
// "badge" : 1, sendig the badge number, will cause aglitch
// receiving device localized parameters
"title_loc_key" : title,
"subtitle_loc_key" : subtitle,
"body_loc_key" : body,
"sound" : true, // or specify audio name to play
],
"data" : [
"data": "ciao",
]
]
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
do {
// request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: JSONSerialization.WritingOptions())
request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: [.prettyPrinted]) // working
print("My paramaters: \(postParams)")
} catch {
print("Caught an error: \(error)")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if let realResponse = response as? HTTPURLResponse {
if realResponse.statusCode != 200 {
print("Not a 200 response")
}
}
if let posData = data {
if let postString = String(data: posData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) as String? {
print("POST: \(postString)")
}
}
}
task.resume()
}
这是函数调用:
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))
【问题讨论】:
标签: ios swift localization firebase-cloud-messaging