本文介绍了UILocalNotification - 每天在特定时间点火和重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this piece of code which I use for firing a local notification each day at mid night:

//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每天发送另一个本地通知。这是如何完成的?我不明白上面的代码如何适应这个... ..

However I need another local notification to fire each day at 13:00. How is this accomplished? I don't understand how the above code can be adapted to achieve this..

非常感谢,

杰克

推荐答案

如果你有时间每天发出通知,你应该这样做

If you have the time which you have to fire notification every day, you should do this

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];

then from here it will connect to the main notification firing method
[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];
}

这篇关于UILocalNotification - 每天在特定时间点火和重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 00:56