Ios 推送通知本地化以发送设备语言显示,而不是以接收设备语言显示。敏捷的
我正在翻译我的应用程序,但远程通知有问题。 下列文件 在应用程序包中存储本地化内容 如果在通知中使用一组一致的消息,则 可以在应用程序包中存储消息文本的本地化版本 并使用有效负载中的loc键和loc args键指定 要显示的消息。loc键和loc args键定义消息 通知的内容。如果存在,本地系统将进行搜索 应用程序的Localizable.strings文件,用于匹配 loc键中的值。然后,它使用 字符串文件作为消息文本的基础,替换任何 带有loc args键指定的字符串的占位符值。 (还可以使用指定通知的标题字符串。) 标题锁定键和标题锁定参数键。) 我有Ios 推送通知本地化以发送设备语言显示,而不是以接收设备语言显示。敏捷的,ios,swift,localization,firebase-cloud-messaging,Ios,Swift,Localization,Firebase Cloud Messaging,我正在翻译我的应用程序,但远程通知有问题。 下列文件 在应用程序包中存储本地化内容 如果在通知中使用一组一致的消息,则 可以在应用程序包中存储消息文本的本地化版本 并使用有效负载中的loc键和loc args键指定 要显示的消息。loc键和loc args键定义消息 通知的内容。如果存在,本地系统将进行搜索 应用程序的Localizable.strings文件,用于匹配 loc键中的值。然后,它使用 字符串文件作为消息文本的基础,替换任何 带有loc args键指定的字符串的占位符值。 (还可以
NSLocalizedString
的标题、副标题和正文。问题是,当我从英语set设备发送通知时,我会在意大利set设备上收到英语远程通知。我将所有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))
我知道你在打电话
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
文件中获取值。
由于myLocalizable.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])
再次感谢。您还需要标题和字幕,确实如此。我不得不更改字符串格式,因为
%1@
无法识别,但是普通%@
很有魅力。