删除计划的本地通知

问题描述 投票:1回答:2

我知道有很多类似的问题和答案,我都对它们进行了审查,但仍然找不到解决方案。尽管从UNUserNotificationCenter中删除了计划的通知,但仍会触发它。

我创建本地通知如下:

let aDF = DateFormatter()
aDF.dateFormat = "yyyy-MM-dd HH:mm:ss"
var identifierST = ""
    if update == true {
        identifierST = myCoreDataEntity.notificationUID!
    } else {
        identifierST = aDF.string(from: Date())
    }

let notif = UNMutableNotificationContent()
notif.title = "some string"
notif.body = "some string"
var dateComp: DateComponents
switch myCoreDataEntity.schedule {
case 1: //daily
    dateComp = Calendar.current.dateComponents([.hour, .minute], from: myCoreDataEntity.date)

case 2: //weekly
    dateComp = Calendar.current.dateComponents([.weekday, .hour, .minute], from: myCoreDataEntity.date)

case 3: //monthly
    dateComp = Calendar.current.dateComponents([.day, .hour, .minute], from: myCoreDataEntity.date)

case 4: //quartely - this is actually not quarterly, dont know how to set quarterly, setting monthly
    dateComp = Calendar.current.dateComponents([.day, .hour, .minute], from: myCoreDataEntity.date)

case 5: //halfyearly - this is actually not halfyearly, dont know how to set halfyearly, setting monthly
    dateComp = Calendar.current.dateComponents([.day, .hour, .minute], from: myCoreDataEntity.date)

case 6: //yearly
    dateComp = Calendar.current.dateComponents([.month, .day, .hour, .minute], from: myCoreDataEntity.date)

default: //monthly
    dateComp = Calendar.current.dateComponents([.day, .hour, .minute], from: myCoreDataEntity.date)

}
dateComp.hour = 10
dateComp.minute = 0

let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true)
        let request = UNNotificationRequest.init(identifier: timeStampST, content: notif, trigger: notificationTrigger)
        UNUserNotificationCenter.current().add(request) { (error) in
            if (error != nil) {
                 print (error) //notify user that reminder was not saved
            } else {
                 myCoreDataEntity.notificationUID = identifierST
            }
        }

[notificationUID是CoreData实体上的字符串属性,我在其中存储创建的通知标识符,因此以后可以检索它。

以上操作正常,通知是按计划的并在定义的日期和时间发送的,因此这里没有问题。

[每当我需要删除特定的通知时,我都会检索保存的通知标识符(notificationUID)并将其传递给以下功能:

func removeExisting(identifierST: String) {
     UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifierST])
}

[尝试查找问题时,我已检索所有待处理的通知,并将它们的identifiers与我传递给identifierSTremovePendingNotificationRequests进行了比较,并且它们匹配

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
        for request in requests {
            print("==============================================")
            print(request.identifier)
            print(request.content.title)
            print(request.content.subtitle)
            print(request.content.body)
        }
})

但是那些取消的通知仍在触发,我在这里遗漏了明显的东西吗?

编辑1

[提供有关应用逻辑和场景的更多详细信息:

  • [App创建一些每天,每周,每月等重复发生的事件

  • 对于每个事件,创建通知

  • 通知在活动日期发送

  • 用户可以看到即将发生的事件,可以选择跳过一个周期=>这是问题所在->当用户选择跳过时,我运行func removeExisting(identifierST: String)来删除它,但是当跳过的日期事件到来时,通知仍在发送。

swift ios13 unusernotificationcenter
2个回答
0
投票

[您的意思是,您致电getPendingNotificationRequests,再也看不到您删除的那个?然后,您确定之后添加的代码不会被意外调用吗? (因此可以再次添加?)

UNUserNotificationCenter.current().add()的一个功能是,如果已经计划了具有所需ID的通知,它将不会同时计划另一个通知,但是也不会给出错误。您可以认为它会覆盖现有的文件。因此,如果您的代码以某种方式一次又一次地调度相同的通知,则您可能不会注意到。除非您尝试删除通知,然后重新安排它们。


0
投票

我现在再次观看了相关的WWDC视频-https://developer.apple.com/videos/play/wwdc2016/707-并稍微改变了逻辑。

原始方法

[当用户跳过一个周期时,我将删除计划的通知并创建一个具有新标识符和新触发日期的新通知。

新方法

[当用户跳过一个周期时,我没有删除任何内容,而是创建了一个具有新触发日期的“新”通知,但是使用了相同的标识符。据我从视频中了解到,使用相同的标识符实际上并不是在创建新的通知,而是在更新现有的通知。我将能够在24小时内确认是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.