【问题标题】:UILocalNotification - Fire and Repeat at particular time each dayUILocalNotification - 每天在特定时间触发和重复
【发布时间】:2012-04-30 03:30:06
【问题描述】:

我有这段代码用于每天午夜触发本地通知:

//Get todays midnight
    NSDate *alertTime = [NSDate date];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
    alertTime = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:alertTime]];

    UIApplication *app = [UIApplication sharedApplication];

    //Set up the local notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    if(notification){
        //Set fire date to alert time
        notification.fireDate = alertTime;
        //Set time zone to default
        notification.timeZone = [NSTimeZone defaultTimeZone];
        //Repeat the notification everyday (fires at same time
        //as initial notification)
        notification.repeatInterval = NSDayCalendarUnit;

        // schedule notification
        [app scheduleLocalNotification:notification];

        NSLog(@"%@", notification.fireDate);
    }

但是,我需要在每天 13:00 触发另一个本地通知。这是如何实现的?我不明白如何调整上面的代码来实现这一点..

非常感谢,

杰克

【问题讨论】:

  • 观看我的回答,namaztimes 是我的数组,其中包含触发时间,在这段代码中,我必须每天触发 5 次本地通知(每天都这样),所以,根据你可以自定义它

标签: iphone objective-c ios uilocalnotification


【解决方案1】:

如果你有时间每天都必须触发通知,你应该这样做

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    for (NSMutableArray * arrDay in self.namaztimes) {
        NSLog(@"Alarm Array: %@", arrDay);
        int count=[arrDay count];
        if(!count){
            continue;
        }

        int day =0;
        int month=0;
        int year=0;
        int hour =0;
        int minutes=0;

        //  NSArray *arrDates=[[NSMutableArray alloc] initWithCapacity:1];
        for ( int i=0;i<3;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            if (i==0) {
                day = [dayTime intValue];    
            }else if(i==1){
                month = [dayTime intValue];                    
            }else if(i==2){
                year = [dayTime intValue];    

            }
        }
        for ( int i=3;i<count;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            hour = [[dayTime substringToIndex:2] intValue];
            minutes = [[dayTime substringFromIndex:3] intValue];

            [components setDay:day];
            [components setMonth:month];
            [components setYear:year];
            [components setMinute:minutes];
            [components setHour:hour];


            NSDate *myNewDate = [calendar dateFromComponents:components];

            [self scheduleNotificationForDate:myNewDate];

        }
    }

    [components release];
    [calendar release];

然后从这里连接到主通知触发方法
[self scheduleNotificationForDate:myNewDate];

-(void) scheduleNotificationForDate: (NSDate*)date {
    /* Here we cancel all previously scheduled notifications */
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Your Notification Text"; //[NSString stringWithFormat:@"%@",date];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1" 
     means that number indicator on the badge will be decreased by one 
     - so there will be no badge on the icon */

    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = -1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

【讨论】:

  • 那是我的数组,它有不同的时间在特定时间触发本地通知,我已经在问题中添加了注释以指定它
  • localNotification.applicationIconBadgeNumber = -1;为什么-1?根据我的经验,使用 repeatInterval 设置的通知不会在应用图标上显示正确的计数?
  • 谁在说徽章号码?我们讨论了安排通知和声音,而不是徽章。
  • @Charan 你能告诉我们你的 self.namaztime 数组吗?我遇到了同样的麻烦,试图找到解决方案但卡住了,所以请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2019-09-11
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多