Local notification UNTimeIntervalNotificationTrigger triggerWithTimeInterval fires every 1 minutes how to stops

5.1k views Asked by At

I am using local notifications in my app to alert urgent messages to the user. What happens is the user receives a push notification, then a local notification is created and fired 60 seconds later with a time interval of 60 seconds. This works great and the urgent notification fires every 60 seconds as expected.

The local notification star firing every one minute. But i want to stop them. Can you suggest me how to handle this.

On iOS 9 we did not experience this problem at all and the notification would fire repeatedly overnight even, so I am thinking this might be something related to iOS 10?

The code I use to create the notification is as follows:

    let content = UNMutableNotificationContent()
            content.body = NSString.localizedUserNotificationString(forKey: notificationMessage, arguments: nil)

            content.badge = 1
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
            let request = UNNotificationRequest.init(identifier: "", content: content, trigger: trigger)
            center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
                if error == nil {
                    print("add NotificationRequest succeeded!")
//                    trigger.timeInterval.
                }
            })
1

There are 1 answers

0
Sankalap Yaduraj Singh On BEST ANSWER

I found the missing point.

  let content = UNMutableNotificationContent()
  content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: notificationMessage, arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber?
  content.categoryIdentifier = "com.elonchan.localNotification"
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
    if error == nil {
        print("add NotificationRequest succeeded!")
        center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"])
    }
})