本地通知UNTimeIntervalNotificationTrigger triggerWithTimeInterval每1分钟触发一次如何停止

时间:2017-01-04 10:19:19

标签: swift ios10 unnotificationrequest

我在我的应用中使用本地通知来向用户发出紧急消息。发生的情况是用户收到推送通知,然后创建本地通知并在60秒后以60秒的时间间隔触发。这很有效,紧急通知按预期每60秒触发一次。

  

本地通知星每分钟开火一次。但是我要   阻止他们。你能建议我怎么处理这个。

在iOS 9上我们根本没有遇到这个问题,通知会在一夜之间反复发出,所以我想这可能与iOS 10有关吗?

我用来创建通知的代码如下:

    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 个答案:

答案 0 :(得分:1)

我找到了遗漏点。

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