如何在 ios 10 中发送通知后添加贪睡效果

How to add snooze effect once a notification is delivered in ios 10

我正在我的应用程序中实施 UserNotification。当通知被触发时,它会显示两个动作,其中一个我想添加贪睡效果,它必须在 5 分钟后再次贪睡。如何处理?谢谢大家!如果有人有想法,请提供帮助

要暂停通知,您可以创建另一个具有与当前通知相同详细信息的通知,并将触发日期增加 5 分钟。

这是我使用的代码:

func snoozeScheduledNotification(notification:UILocalNotification) -> Void {
    // Snooze for 10 mins
    let localNotification = UILocalNotification()
    localNotification.fireDate = notification.fireDate?.addingTimeInterval(60*10) 
    localNotification.repeatInterval = NSCalendar.Unit(rawValue: 0) // 0 = No Repeat
    localNotification.alertBody = notification.alertBody
    localNotification.soundName = notification.soundName
    localNotification.userInfo = notification.userInfo  
    localNotification.category = notification.category
    UIApplication.shared.scheduleLocalNotification(localNotification)
}

希望对您有所帮助。

对于 iOS10,使用此代码。

AppDelegate.swift 文件中使用此代码。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    let center = UNUserNotificationCenter.current()
    let category = UNNotificationCategory(identifier: "identifier", actions: [], intentIdentifiers: [])
    center.setNotificationCategories([category])
    center.requestAuthorization(options: [.badge, .alert , .sound]) { (greanted, error) in
            print(error)
    }
    return true
}

您可以将这段代码放在任何视图控制器中。

let content = UNMutableNotificationContent.init()
content.title = "Notification Title"
content.subtitle = "Notification Sub-Title"
content.body = "Notification Body"
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { error in 
    UNUserNotificationCenter.current().delegate = self
    if (error != nil){
        //handle here
    }
}

您可以使用以下方法处理通知:

extension UIViewController: UNUserNotificationCenterDelegate {

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
        completionHandler( [.alert, .badge, .sound])
    }

    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
        print("Tapped in notification")
    }
}

你可以使用这个Blog as reference and Example

我找到的最短最简单的代码

对于Swift 3/4

extension UNNotification {
    func snoozeNotification(for hours: Int, minutes: Int, seconds: Int) {
        let content = UNMutableNotificationContent()

        content.title = "Another Alert"
        content.body = "Your message"
        content.sound = .default()

        let identifier = self.request.identifier
        guard let oldTrigger = self.request.trigger as? UNCalendarNotificationTrigger else {
            debugPrint("Cannot reschedule notification without calendar trigger.")
            return
        }

        var components = oldTrigger.dateComponents
        components.hour = (components.hour ?? 0) + hours
        components.minute = (components.minute ?? 0) + minutes
        components.second = (components.second ?? 0) + seconds

        let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                debugPrint("Rescheduling failed", error.localizedDescription)
            } else {
                debugPrint("rescheduled success")
            }
        }
    }

}

你只需要这样称呼它:

response.notification.snoozeNotification(for: 0, minutes: 0, seconds: 30)

归功于 Simon Ljungberghttps://gist.github.com/simme/96264d5ceee394083d18e2c64f42a3a9