首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理超过64个的多个UILocalnotification

处理超过64个的多个UILocalnotification
EN

Stack Overflow用户
提问于 2015-01-02 12:55:51
回答 5查看 1.9K关注 0票数 3

请用代码解释一下!可能是这个问题的复制品,Manage Multiple UILocal Notification,但还没有得到答案?请将注意力集中在这个问题上,并回答我如何管理超过64的通知?

我有所有UINotification时间的本地数据库,比如下面的问题:

Set multiple UILocalNotification

请回答。

EN

回答 5

Stack Overflow用户

发布于 2015-01-04 16:10:51

无法在任何给定时间安排超过64个本地通知。然而,也许有一些方法可以有效地实现你想要的东西。

  1. 如果您有重复的通知,请不要手动安排。请改用UILocalNotification对象上的repeatInterval。此属性获取一个NSCalendarUnit,可用于将重复设置为每小时、每天、每月等。
  2. 每次启动应用程序时,重新评估计划的UILocalNotification对象。丢弃所有不再有效的提醒,并在将来尽可能多地排队。
  3. 这是最不优雅的解决方案,但可能是为用户设置大量提醒的最佳解决方案。但是,请注意,在尝试此方法之前,您实际上不会注册额外的UILocalNotification对象,因此当显示通知时,您的用户将不会被带到应用程序。这样,您就可以使用calendar events and reminders为用户设置提醒。您可能希望使用EventKit框架来处理所有繁重的工作。这种方法的优点是,您可以根据需要安排尽可能多的事件。你可以使用日历作为你的通知的后备存储器。因此,每次启动应用程序时,您都会使用其中一个谓词方法(如- (NSArray *)eventsMatchingPredicate:(NSPredicate *)predicate)查询EKEventStore,以查找您创建的事件。
票数 4
EN

Stack Overflow用户

发布于 2015-01-02 16:48:54

也许还有其他方法可以管理UILocalNotification。根据每个苹果,你可以设置最多64个通知。因此,要管理超过64个通知,您可以实现以下逻辑。

首先,注册前64个本地通知。在每次应用程序启动或didBecomeActive时,使用[[UIApplication sharedApplication]scheduledLocalNotifications]获取剩余通知或计数。因此,如果您的计数<64,那么就说它是30。因此,添加新的34个通知,现在您的通知总数将为64。每次启动时都要这样做,这样它就可以根据您的需要工作。

也许这会对你有帮助。

票数 1
EN

Stack Overflow用户

发布于 2015-01-08 21:00:40

试试这个,你可以通过选择setDaysetMonth,...来改变间隔。:

代码语言:javascript
复制
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];

NSDate *date3Days = [calendar dateByAddingComponents:components 
                                               toDate:[NSDate date] 
                                              options:0];

UIApplication* app = [UIApplication sharedApplication];
NSArray*    oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
    [app cancelAllLocalNotifications];
    app.applicationIconBadgeNumber = 0;
}

UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
    notifyAlarm.fireDate = date3Days;
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    notifyAlarm.alertBody = NSLocalizedString( @"Push message", @"");
    notifyAlarm.soundName = @"sound.wav";
    [app scheduleLocalNotification:notifyAlarm];
}

如果您想设置一个特定的时间,在该时间之后UILocalNotifications应该出现,您可以创建上述解决方案的一个方法,并循环遍历一个数组,该数组指示您希望显示通知的日期:

代码语言:javascript
复制
NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120];
NSDictionary *dictNotifications = 
[[NSUserDefaults standardUserDefaults]    objectForKey:kUserDefAppStarts];

for ( NSNumber *bla in arrayNumbers ){

    NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla];
    NSDictionary *dict = dictNotifications[strKey];
    NSString *strMessageQuestion = dict[kKeyMessage];

    [self createNotificationWithNumberOfDays:[bla integerValue]
                                  andMessage:strMessageQuestion
                                    userInfo:dict];
}

下面是您必须调用的方法

代码语言:javascript
复制
+ (void)createNotificationWithNumberOfDays:(int)days 
                                andMessage:(NSString *)message 
                                  userInfo:(NSDictionary *)dict{

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];

NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];

UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];

if( notifyAlarm ){
    [notifyAlarm setFireDate:dateAlert];
    [notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
    [notifyAlarm setSoundName:@"soundname"];
    [notifyAlarm setAlertBody:message];
    [notifyAlarm setUserInfo:dict];
    [app scheduleLocalNotification:notifyAlarm];
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27737529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档