Question: Local notification UNTimeIntervalNotificationTrigger triggerWithTimeInterval fires every 1 minutes how to stops

Question

Local notification UNTimeIntervalNotificationTrigger triggerWithTimeInterval fires every 1 minutes how to stops

Answers 1
Added at 2017-01-04 11:01
Tags
Question

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.
                }
            })
Answers to

Local notification UNTimeIntervalNotificationTrigger triggerWithTimeInterval fires every 1 minutes how to stops

nr: #1 dodano: 2017-01-04 13:01

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"])
    }
})
Source Show
◀ Wstecz