【问题标题】:Push notification Localization are displayed in the sending device language, not in the receiving one. Swift推送通知本地化以发送设备语言显示,而不是接收设备语言。迅速
【发布时间】: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


    【解决方案1】:

    我知道你打电话给我

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

    您正在执行本地化,而不是发送本地化密钥。

    【讨论】:

    • 你还需要 title_loc_args 和相同的字幕
    • 确实是这样。面团我不得不将字符串格式更改为 %1@ 不被识别,但简单的 %@ 可以作为一种魅力。
    【解决方案2】:

    我发布了一个答案,以便为其他偶然发现此问题的人做一个很好的回顾。 正如接受的答案中所述,主要问题是我传递了“本地化”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])
    

    再次感谢您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多