0

I am implementing UserNotification in my app. When the notification gets fired it shows two action, in one i want to add snooze effect, it must snooze after 5 mins again. How to handle it ? thanks for all ! help if any one do have idea

kishan
  • 203
  • 1
  • 6
  • 15

3 Answers3

0

Well to snooze notification you can create another notification with same details of current notification and increase the fire date by 5 mins.

Here is the code I used :

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)
}

Hope it helps you.

Malav Soni
  • 2,739
  • 1
  • 23
  • 52
0

The shortest and simplest code I found about it

For 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")
            }
        }
    }

}

You just need to call it this way :

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

Credit goes to Simon Ljungberg : https://gist.github.com/simme/96264d5ceee394083d18e2c64f42a3a9

Mehdi Chennoufi
  • 2,193
  • 2
  • 11
  • 16
-1

For iOS10, use this code.

Use this code in AppDelegate.swift file.

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
}

You can put this code in any view controller.

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
    }
}

You can handle notification using following method:

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")
    }
}

You can use this Blog as reference and Example.

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32