当重复设置为“是 - Swift”时,UNUser通知不会从挂起的通知中删除



我一直在构建剩余的应用程序设置,无论是否具有重复模式,都可以在特定时间

进行剩余。

我一直在努力解决一个问题。 任何帮助都将有助于解决它。

这是我面临的场景

当在没有重复模式

的情况下创建其余部分并且我正在删除其余部分时,挂起的通知将被删除,但是当将其设置为重复模式时,挂起的通知不会被删除。

这是我删除通知的代码。

UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
            let allNotifIds = requests.map({ $0.identifier })
            let pendingTaskNotifIds = taskNotificationIds.filter({ allNotifIds.contains($0) })
            UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: pendingTaskNotifIds)
        }

添加通知的代码

  let content = UNMutableNotificationContent()
    //set badge content here (get list from db and update it.)
    content.badge = NSNumber(value: getBadgeCount())
    content.title="sometitle";
    content.body = "body data"
    content.sound = UNNotificationSound.default
    let twoWeeksTrigger =  UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval.everyTwoWeeks, repeats: true)
    let id = UUID().uuidString
    let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request)

这是我的日志,我创建了 2 个余数(1 个是非重复任务,另一个是重复任务)

[<UNNotificationRequest: 0x600003dd5440; identifier: 06B35F80-9769-492A-8BFF-3108A11B1542, content: <UNNotificationContent: 0x600000786700; title: , subtitle: (null), body: Without repeat notification, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: com.example, launchImageName: , threadIdentifier: , attachments: (
), badge: 1, sound: <UNNotificationSound: 0x60000163b180>,, trigger: <UNCalendarNotificationTrigger: 0x6000033d7f40; dateComponents: <NSDateComponents: 0x6000005a0f20>
    Calendar Year: 2019
    Month: 1
    Day: 22
    Hour: 13
    Minute: 53
    Weekday: 3, repeats: NO>>, 
<UNNotificationRequest: 0x600003dd3ae0; identifier: D33A49EC-DD3C-4090-AD86-9F16ED4EA6CC, content: <UNNotificationContent: 0x6000007852c0; title: , subtitle: (null), body: Repeat notification, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: com.example, launchImageName: , threadIdentifier: , attachments: (
), badge: 1, sound: <UNNotificationSound: 0x600001639800>,, trigger: <UNCalendarNotificationTrigger: 0x6000033d5740; dateComponents: <NSDateComponents: 0x6000005a1ef0>
    Month: 1
    Day: 22
    Hour: 13
    Minute: 54, repeats: YES>>]
删除两个任务后,将触发重复任务通知,

并且我的通知对象在日志中打印了重复任务对象,仅删除非重复任务。这是供参考的日志。

[<UNNotificationRequest: 0x600003d2b300; identifier: D33A49EC-DD3C-4090-AD86-9F16ED4EA6CC, content: <UNNotificationContent: 0x60000079ca80; title: , subtitle: (null), body: Repeat notification, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: com.lawrence.revival.category, launchImageName: , threadIdentifier: , attachments: (
), badge: 1, sound: <UNNotificationSound: 0x60000163fde0>,, trigger: <UNCalendarNotificationTrigger: 0x6000033f9a40; dateComponents: <NSDateComponents: 0x6000005be470>
    Month: 1
    Day: 22
    Hour: 13
    Minute: 54, repeats: YES>>]

根据我的想法,您的标识符是不同的。

首先,检查您的标识符是否相同

参考此链接

https://stackoverflow.com/a/40563595/2254673

最新更新