如何检测在 Swift 4 中触发预定通知的时间
How to detect when a scheduled notification has been fired in Swift 4
我已经创建了一个预定的通知,我希望能够在它发送后进行处理。不是在点击或 selected 之后,而是在实际交付并显示在用户屏幕上时。
我用来生成通知的代码是:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "foo", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "bar", arguments: nil)
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
print(dateInfo.hour!)
print(dateInfo.minute!)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
// Create the request object.
let notificationRequest = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
center.add(notificationRequest)
我确定我必须向我的 AppDelegate 添加一些东西,以便在发送通知时做出响应,但我一直在整个互联网上寻找,但找不到在送货而不是 select.
当通知到达时,系统调用 UNUserNotificationCenter 对象的委托的 userNotificationCenter(_:willPresent:withCompletionHandler:)
方法。
let center = UNUserNotificationCenter.current()
center.delegate = self // What delegation target Here is my AppDelegate
extension AppDelegate : UNUserNotificationCenterDelegate {
// while your app is active in forground
// Handle Notifications While Your App Runs in the Foreground
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Change this to your preferred presentation option
// Play a sound.
// completionHandler(UNNotificationPresentationOptions.sound)
}
// While App is inactive in background
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// While App is inactive in background
print(userInfo)
completionHandler()
}
}
我已经创建了一个预定的通知,我希望能够在它发送后进行处理。不是在点击或 selected 之后,而是在实际交付并显示在用户屏幕上时。
我用来生成通知的代码是:
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "foo", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "bar", arguments: nil)
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
print(dateInfo.hour!)
print(dateInfo.minute!)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
// Create the request object.
let notificationRequest = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
center.add(notificationRequest)
我确定我必须向我的 AppDelegate 添加一些东西,以便在发送通知时做出响应,但我一直在整个互联网上寻找,但找不到在送货而不是 select.
当通知到达时,系统调用 UNUserNotificationCenter 对象的委托的 userNotificationCenter(_:willPresent:withCompletionHandler:)
方法。
let center = UNUserNotificationCenter.current()
center.delegate = self // What delegation target Here is my AppDelegate
extension AppDelegate : UNUserNotificationCenterDelegate {
// while your app is active in forground
// Handle Notifications While Your App Runs in the Foreground
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Change this to your preferred presentation option
// Play a sound.
// completionHandler(UNNotificationPresentationOptions.sound)
}
// While App is inactive in background
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// While App is inactive in background
print(userInfo)
completionHandler()
}
}