我正在尝试添加自定义本地通知,但是我只能通过操作获取库存通知:
我的故事板如下所示(标准模板):
我有一个扩展名已UNNotificationExtensionCategory
设置为awesomeNotification
(在Info.plist中)。该扩展的基础也是的Notification Content
模板iOS - Application Extension
。
在我的应用程序委托中,我有以下内容:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let center = UNUserNotificationCenter.current()
let actions = [UNNotificationAction.init(identifier: "Hey", title: "Yo", options: UNNotificationActionOptions.foreground)]
let category = UNNotificationCategory(identifier: "awesomeNotification", actions: actions, minimalActions: actions, intentIdentifiers: [], options: [])
center.setNotificationCategories([category])
center.requestAuthorization([.alert, .sound]) { (granted, error) in
}
return true
}
在主应用程序的viewcontroller中,我可以执行以下操作来触发它:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.categoryIdentifier = "awesomeNotification"
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
编辑
更新:从iOS 10 beta 2开始,丰富的通知在3D之前的触摸设备上也可用。下拉常规通知以查看它。
确保您正在iPhone6s / iPhone6s plus模拟器/设备上进行测试,但它似乎无法在3D之前的触摸设备上运行。
在iPhone6模拟器上,尝试单击并向下拖动获得的股票通知,您应该会看到自定义UI出现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句