ios - 如何在completionHandler中返回字符串值?
问题描述
我有一个UNNotificationPresentationOptions类型的完成处理程序,现在我想返回字符串值,而不是 .alert。
我想在通知中显示阿拉伯语文本,所以我想在 completionHandler中设置msg值
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print(userInfo)
let userInfoa = notification.request.content
let sd = userInfoa.title
if let jsonResult = userInfo as? Dictionary<String, AnyObject> {
var msg = ""
if let aps_Data = userInfo as? Dictionary<String, AnyObject> {
if let ar_message = aps_Data["gcm.notification.body_ar"] {
print(ar_message)
msg = ar_message as! String
}
}
let content:UNNotificationPresentationOptions = msg
completionHandler([content])
//completionHandler([.alert]) . *I dont want use .alert
}
}
}
解决方案
看声明
@available(iOS 10.0, *)
public struct UNNotificationPresentationOptions : OptionSet {
public init(rawValue: UInt)
public static var badge: UNNotificationPresentationOptions { get }
public static var sound: UNNotificationPresentationOptions { get }
public static var alert: UNNotificationPresentationOptions { get }
}
这些是权限类型 .alert /.sound/.badge ,您不能将委托方法签名更改为您想要的,其主要目的是返回系统将为即将到来的通知触发的权限
//
您可以使用通知服务 && 内容扩展
在此处实施您的编辑
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
contentHandler(bestAttemptContent)
}
}
推荐阅读
- java - Java MongoDB 在数据存在时为 find() 返回 null
- node.js - nodejs , mongo find 没有返回数据
- html - 对齐配置文件标题栏
- jquery - 使用 jQuery 制作可切换的导航栏
- activerecord - 谁能用一个例子详细解释多态关联?
- cuda - cuda.jit 矩阵乘法崩溃
- tabulator - 如何通过 ajax:Response 从返回中访问制表列以进行格式化
- javascript - 如何在不移动的本机反应中安排带有滚动视图的静态按钮?
- javascript - 在 JavaScript 数组中搜索与字符串模式匹配的所有项目的计数
- python - Pandas 数据读取器时间限制