0

It could be duplicate of Question asked - Repeating local notification daily at a set time with swift But UILocalNotifications are deprecated iOS 10

I am working on alarm app, I need 2 things 1. local notification on a time 2. Repeat after a time interval

/// Code used to set notification
let content = UNMutableNotificationContent()
            content.body = NSString.localizedUserNotificationString(forKey: titleOfNotification, arguments: nil)
content.userInfo=[]

Code That work fine hit notification at exact time

/* ---> Working Fine --> how i can repeat this after 60 second if untouched
   let triggerDaily = Calendar.current.dateComponents([.hour,.minute,], from: dates)
   let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: weekdays.isEmpty == true ? false : true)
*/


/// ---> Making it Repeat After Time - How Time is passed here ?
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)

/// ---> Adding Request
let request = UNNotificationRequest(identifier:dateOfNotification, content: content, trigger: trigger)                   UNUserNotificationCenter.current().add(request){(error) in
                        if (error != nil){
                            print(error?.localizedDescription ?? "Nahi pta")
                        } else {
                            semaphore.signal()
                            print("Successfully Done")
                        }
                    }

How I can achieve both things at same time ?

Jatin Garg
  • 206
  • 1
  • 12

1 Answers1

0

You can use UNUserNotificationCenter for local notifications, meanwhile, for the repeating notification you can use the performFetchWithCompletionHandler method, for this method you have to set the minimum time interval for the method to be called.

Please follow the link for more details - https://developer.apple.com/documentation/uikit/core_app/managing_your_app_s_life_cycle/preparing_your_app_to_run_in_the_background/updating_your_app_with_background_app_refresh

ALSO Sample code -

 func scheduleLocalNotification(subtitle: String, description: String, offerID: String) {
    // Create Notification Content
    let notificationContent = UNMutableNotificationContent()

    // Configure Notification Content
    notificationContent.title = "New lead for " + subtitle
    notificationContent.body = description
    notificationContent.userInfo = ["aps":
        ["alert": [
            "title":  subtitle,
            "body": description,
            "content-available": "1"
            ],
         "ofrid": offerID,
         "type": "BLBGSync",
         "landinguri": "abc.com",
        ]
    ]



    // Create Notification Request

    let triggertime = UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: false)

    let notificationRequest = UNNotificationRequest(identifier: "YOUR_IDENTIFIER", content: notificationContent, trigger: triggertime)


    // Add Request to User Notification Center
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in
        if let error = error {
            print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
        }
    }
}
Shivam Gaur
  • 491
  • 5
  • 16
  • UNTimeIntervalNotificationTrigger will this trigger notification after 60 Min (3600 Second) from current Time ? – Jatin Garg Feb 19 '19 at 04:11
  • Yes the notification will trigger after 60 min.Plz go through the link for more clarity - https://www.hackingwithswift.com/example-code/system/how-to-set-local-alerts-using-unnotificationcenter – Shivam Gaur Feb 19 '19 at 13:19