Select 显示推送通知的参数
Select displayed arguments of push notification
我收到如下所示的推送通知:
{
"aps" : {
"alert" : {
"loc-key" : "localized_pn_text",
"loc-args" : [ "France", "Frank"]
},
"sound" : "chime.aiff"
},
"acme" : "foo"
}
在我的 Localizable.strings
中,我现在可以添加一个带有关键字 "localized_pn_text"
的字符串来本地化推送通知。
有没有办法订购或select特定参数。例如,我可以将 "Frank, you got a push notification"
显示为 localised 文本吗?
您可以简单地在 dictionary
中添加自定义键,例如
{
"aps" : {
"alert" : {
"loc-key" : "localized_pn_text",
"loc-args" : [ "France", "Frank"]
},
"sound" : "chime.aiff"
},
"acme" : "foo",
"customText": "Frank, you got a push notification"
}
使用
读取customText
的值
if let customText = notification.request.content.userInfo["customText"] as? String {
print(customText)
}
您有两个选择:
在您的 Localizable.strings
中添加 "localized_pn_text" = "%@, you got a push notification"
,然后只发送 "Frank" 作为 "loc-args"
("loc-args" : [ "Frank"]
)
实现一个Notification Service Extension来拦截推送通知并以编程方式创建其内容,下面是一个示例:
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
handleContent(bestAttemptContent: bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
handleContent(bestAttemptContent: bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
func handleContent(bestAttemptContent: UNMutableNotificationContent) {
if let data = bestAttemptContent.userInfo as? [String: Any], let aps = bestAttemptContent.userInfo["aps"] as? [String: Any], let alert = aps["alert"] as? [String: Any] {
if let locKey = alert["loc-key"] as? String, let locArgs = alert["loc-args"] as? [String] {
bestAttemptContent.body = //Uptaded notification text
}
}
}
}
实际上,您可以 select 参数 %<parameter index starting with 1>$@
例如,我可以在 Localizable.strings
文件中写入:
"localized_pn_text" = "%2$@, you got a push notification"
我收到如下所示的推送通知:
{
"aps" : {
"alert" : {
"loc-key" : "localized_pn_text",
"loc-args" : [ "France", "Frank"]
},
"sound" : "chime.aiff"
},
"acme" : "foo"
}
在我的 Localizable.strings
中,我现在可以添加一个带有关键字 "localized_pn_text"
的字符串来本地化推送通知。
有没有办法订购或select特定参数。例如,我可以将 "Frank, you got a push notification"
显示为 localised 文本吗?
您可以简单地在 dictionary
中添加自定义键,例如
{
"aps" : {
"alert" : {
"loc-key" : "localized_pn_text",
"loc-args" : [ "France", "Frank"]
},
"sound" : "chime.aiff"
},
"acme" : "foo",
"customText": "Frank, you got a push notification"
}
使用
读取customText
的值
if let customText = notification.request.content.userInfo["customText"] as? String {
print(customText)
}
您有两个选择:
在您的
Localizable.strings
中添加"localized_pn_text" = "%@, you got a push notification"
,然后只发送 "Frank" 作为"loc-args"
("loc-args" : [ "Frank"]
)实现一个Notification Service Extension来拦截推送通知并以编程方式创建其内容,下面是一个示例:
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
handleContent(bestAttemptContent: bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
handleContent(bestAttemptContent: bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
func handleContent(bestAttemptContent: UNMutableNotificationContent) {
if let data = bestAttemptContent.userInfo as? [String: Any], let aps = bestAttemptContent.userInfo["aps"] as? [String: Any], let alert = aps["alert"] as? [String: Any] {
if let locKey = alert["loc-key"] as? String, let locArgs = alert["loc-args"] as? [String] {
bestAttemptContent.body = //Uptaded notification text
}
}
}
}
实际上,您可以 select 参数 %<parameter index starting with 1>$@
例如,我可以在 Localizable.strings
文件中写入:
"localized_pn_text" = "%2$@, you got a push notification"