0

我在我的应用程序中使用本地通知向用户提醒紧急消息。发生的情况是用户收到推送通知,然后创建本地通知并在 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.
                }
            })
4

1 回答 1

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"])
    }
})
于 2017-01-04T12:54:08.930 回答