gpt4 book ai didi

iphone - 在特定时间取消所有通知

转载 作者:行者123 更新时间:2023-11-29 13:23:30 25 4
gpt4 key购买 nike

对于我的应用程序,我不希望在周末关闭通知。所以我的想法是在星期五的特定时间取消所有通知,我想通过安排任务来做到这一点,就像我安排通知一样(UILocalNotifications)

例如,我的闹钟代码如下:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:8];
[comps setMinute:25];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *fireDate = [gregorian dateFromComponents:comps];

UILocalNotification *alarm = [[UILocalNotification alloc] init];

alarm.fireDate = fireDate;
alarm.repeatInterval = NSDayCalendarUnit;
alarm.soundName = @"sound.aiff";
alarm.alertBody = @"Message..";
alarm.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];

有没有办法用同样的方法取消所有的通知,或者苹果不允许这样做?

最佳答案

您可以通过以下方式取消本地通知:

[[UIApplication sharedApplication] cancelLocalNotification:notification];

您需要遍历所有本地通知并在周末取消它们。循环遍历它们:

NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[notifications enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop){
if (/* notification.fireDate is on weekend */) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}];

但是,您必须确保应用程序在星期五运行才能执行删除周末通知的代码。

但是,为什么不一开始就安排周末呢?

关于iphone - 在特定时间取消所有通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com