我正在尝试添加自定义本地通知,但我仅通过我的操作获得库存通知:
我的故事板看起来像这样(标准模板):
我有一个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
}
在主应用程序的视图控制器中,我有以下操作来触发它:
@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")
}
编辑