相关疑难解决方法(0)

在iOS 10中使用UNUserNotificationCenterDelegate收到新通知时,如何删除以前发送的通知?

这个问题是关于UserNotificationsiOS 10中的新框架.

我有一个应用程序,在用户在应用程序中执行某个操作后,每隔半小时安排一次本地通知,从1小时开始.

为了避免使用户的锁定屏幕或通知中心混乱,我只希望一次显示一个通知,因此只有一个通知包含最新的相关信息.我实现这一目标的计划是在出现新通知时随时清除所有已发送的通知.

看来这应该是可行的新willPresent方法UNUserNotificationCenterDelegate,但它的行为并不像我期望的那样.

这是一个函数,我打电话设置所有通知,从应用程序中的事件后1小时开始,并在事件发生后23.5小时每半小时安排一次通知,直到最后一个通知:

func updateNotifications() {

    for hour in 1...23 {
        scheduleNotification(withOffsetInHours: Double(hour))
        scheduleNotification(withOffsetInHours: Double(hour) + 0.5)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是根据实际进度通知的功能mostRecentEventDate,这是一个Date集内其他:

func scheduleNotification(withOffsetInHours: Double) {

    // set up a Date for when the notification should fire
    let offsetInSeconds = 60 * 60 * withOffsetInHours
    let offsetFireDate = mostRecentEventDate.addingTimeInterval(offsetInSeconds)

    // set up the content of the notification
    let content = UNMutableNotificationContent()
    content.categoryIdentifier = "reminder"
    content.sound = UNNotificationSound.default()
    content.title = …
Run Code Online (Sandbox Code Playgroud)

notifications ios ios10 unusernotificationcenter

11
推荐指数
1
解决办法
4651
查看次数