0

How can i push a local notification at specific time (ex: 9:00AM) in specific N day get from DB. I tried timer to add a Notification but it's seem like Timer is suppense when i minimize app. Here's my code:

func createTriggerLocalPushNotification(completion: @escaping(Error?) -> ()) {

    var date = Date()
    date.hour = 12
    date.minute = 0
    date.second = 0
    var timeInterval = 86400 * 3 // Default 3 day
    var timeBuffer:TimeInterval = 0
    guard let inActivePushString = RealmUserInfo.shared.getValue(with: keyInActiveTimePush),
        let inActivePushTime = Int(inActivePushString), inActivePushTime > 0 else {
        log.info("Can't trigger inActivePushTime")
            return
    }

    date.add(.day, value: inActivePushTime)

    if #available(iOS 10.0, *) {
        timeInterval = 86400 * inActivePushTime
        timeBuffer = date.timeIntervalSinceNow

        let content = UNMutableNotificationContent()
        content.body = self.contentMessage
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = self.inActiveTimePushNotificationCategory

        // Buffer trigger when first time
        var dateComponents = DateComponents()
        dateComponents.day = Date().day + inActivePushTime
        dateComponents.hour = 12
        dateComponents.minute = 0
        let firstTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
        let requestBuffer = UNNotificationRequest(identifier: self.bufferInActiveTimePushNotification, content: content, trigger: firstTrigger)
        UNUserNotificationCenter.current().add(requestBuffer, withCompletionHandler: nil)

        // Create content
        Timer.scheduledTimer(withTimeInterval: timeBuffer, repeats: false) { (timer) in
            // Create trigger
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timeInterval), repeats: true)
            let request = UNNotificationRequest(identifier: self.inActiveTimePushNotification, content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
            }
        completion(nil)
    } else {
        // Fallback on earlier versions
        let content = UILocalNotification()
        content.alertBody = contentMessage
        content.soundName = UILocalNotificationDefaultSoundName
        content.category = inActiveTimePushNotification
        content.fireDate = date
        content.repeatInterval = .day
        UIApplication.shared.scheduleLocalNotification(content)
        completion(nil)
    }
}
Lukas Würzburger
  • 6,543
  • 7
  • 41
  • 75

1 Answers1

0

Set Local push notification after 3 days with specific time like 10:00 PM, When user open application OR just enter the app in foreground every time we can get applicationDidBecomeActive delegate method, In this method you need to remove old local push notification if exist and reset after 3 days with a specific time.

See this post to set local push notification with N days. Repeating local notifications for specific days of week (Swift 3 IOS 10)

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • In those articles, it look like we could get date components by weekday, day...etc, and that's not helping me. Currently i can set a notification in the next N day. But i don't know how to automatically reset it. Like this: var dateComponents = DateComponents() dateComponents.day = Date().day + 3 dateComponents.hour = 12 dateComponents.minute = 0 let firstTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false) – Quốc Anh Lê Feb 26 '19 at 03:11