在 ios10 - swift 3 中添加自定义本地通知
Add Custom Local Notification in ios10 - swift 3
我正在尝试添加自定义本地通知,但我的操作仅收到股票通知:
我的故事板看起来像这样(标准模板):
我有一个扩展程序 UNNotificationExtensionCategory
设置为 awesomeNotification
(在 Info.plist 中)。此扩展的基础也是来自 iOS - Application Extension
.
的 Notification Content
模板
在我的应用委托中,我有这个:
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")
}
编辑
所以它适用于 iPhone 6s/6s+,非常奇怪的行为:
更新:从 iOS 10 beta 2 开始,丰富的通知也可在 pre-3D 触摸设备上使用。下拉常规通知可以看到
确保您在 iPhone6s/iPhone6s 加上 simulator/device 上进行测试,它似乎不适用于 pre-3D 触摸设备。
在 iPhone6 模拟器上,尝试单击并向下拖动您收到的股票通知,您应该会看到您的自定义 UI 出现。
我正在尝试添加自定义本地通知,但我的操作仅收到股票通知:
我的故事板看起来像这样(标准模板):
我有一个扩展程序 UNNotificationExtensionCategory
设置为 awesomeNotification
(在 Info.plist 中)。此扩展的基础也是来自 iOS - Application Extension
.
Notification Content
模板
在我的应用委托中,我有这个:
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")
}
编辑
所以它适用于 iPhone 6s/6s+,非常奇怪的行为:
更新:从 iOS 10 beta 2 开始,丰富的通知也可在 pre-3D 触摸设备上使用。下拉常规通知可以看到
确保您在 iPhone6s/iPhone6s 加上 simulator/device 上进行测试,它似乎不适用于 pre-3D 触摸设备。
在 iPhone6 模拟器上,尝试单击并向下拖动您收到的股票通知,您应该会看到您的自定义 UI 出现。