我一直在构建剩余应用程序设置剩余在特定时间有或没有重复模式。
我一直在努力解决一个问题。任何帮助都有助于解决它。
这是我面临的情况
当剩余部分是在没有重复模式的情况下创建的并且我正在删除剩余部分时,挂起的通知将被删除,但是当其设置为重复模式时,挂起的通知不会被删除。
这是我删除通知的代码。
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>>]