推送通知本地化以发送设备语言显示,而不是以接收设备语言显示.迅速



我正在翻译我的应用程序,但远程通知出现问题。 以下文档

在应用程序捆绑包中存储本地化内容

如果您为通知使用一组一致的消息,则 可以在应用捆绑包中存储消息文本的本地化版本 并使用有效负载中的 loc-key 和 loc-args 键来指定哪个 要显示的消息。loc-key 和 loc-args 键定义消息 通知的内容。如果存在,本地系统将搜索 应用的 Localizable.string 文件,用于匹配 位置键中的值。然后,它使用 字符串文件作为消息文本的基础,替换任何 占位符值,其中包含由 loc-args 键指定的字符串。 (您还可以使用 标题定位键和标题定位参数键。

我有标题、副标题和正文NSLocalizedString。问题是,当我从英语设置设备发送通知时,我在意大利语设置设备上收到英语远程通知。我将Localizedkey:value对存储在Localizable.string文件中,以便据我了解它可以工作,在收到通知时,当前设备语言的值应从Localizable.string文件中获取并显示。我对所有警报 pup 都做了同样的事情,它在那里完美运行,但在远程通知上它保留了发送设备的语言。我正在使用 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))

我明白了你打电话

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))

您正在进行本地化,而不是发送用于本地化的密钥。

我发布一个答案,以便为其他偶然发现这一点的人做一个很好的回顾。 如接受的答案中所述,主要问题是我传递了一个"本地化"String而不是"参考"String,以便本地化从正确的Localizable.string文件中获取值。 由于我的Localizable.string字符串具有格式,因此还需要loc-args与有效负载一起传递,以便将占位符交换为值。我有%1@%2@等形式,但正确的形式是简单明了的%@。 所以修改后的代码是:

static func sendPushNotification(to receiverToken: String, title: String, titleArgs: [String], subtitle: String, subtitleArgs: [String], body: String, bodyArgs: [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,
"title_loc_args" : titleArgs,
"subtitle_loc_key" : subtitle,
"subtitle_loc_args" : subtitleArgs,
"body_loc_key" : body,
"body_loc_args" : bodyArgs,
"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: "BOOKING_DELETED_PUSH_TITLE", titleArgs: [self.bookingId], subtitle: "BOOKING_DELETED_PUSH_SUBTITLE", subtitleArgs: [UserDetails.fullName!], body: "BOOKING_DELETED_PUSH_BODY", bodyArgs: [customerName])

再次感谢。

相关内容

  • 没有找到相关文章

最新更新