本地通知问题iphone

时间:2013-10-02 12:09:18

标签: iphone ios apple-push-notifications uilocalnotification

我想制作本地通知,每天通知用户5次,每天重复一次, 我把它们放在一个可变阵列中,对象是“hh:mm”,GMT + 3镇的小时和分钟是固定的,所以我得到当前日期并找到间隔,然后创建通知的日期 这是我实施的方法。 - 第一个申请时区, - 如果在当前时间之前的时间,则为第二天。 - 第三次设置该日期的本地通知。 请帮助我

piece of code

1 个答案:

答案 0 :(得分:0)

使用这个示例代码,我已安排在早上7点和晚上6点的一个通知,并且每天重复一次,并且它非常有效,希望您可以通过使用它来找到您的解决方案。

#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}

-(void)scheduleNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:@"ParsingResponse"];    

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        [components setHour:7];
        [components setMinute:0];
        NSDate *today7am = [calendar dateFromComponents:components];
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = today7am;
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.repeatCalendar = [NSCalendar currentCalendar];
        notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Morning"];
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.repeatInterval = NSDayCalendarUnit;
        NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Morning", @"key", nil];
        notif.userInfo = infoDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];


        NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        [components2 setHour:18];
        [components2 setMinute:0];
        NSDate *today6pm = [calendar2 dateFromComponents:components2];

        UILocalNotification *notif2 = [[cls alloc] init];
        notif2.fireDate = today6pm;
        notif2.timeZone = [NSTimeZone defaultTimeZone];
        notif2.repeatCalendar = [NSCalendar currentCalendar];
        notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:@"Noti_Evening"];
        notif2.alertAction = @"Show me";
        notif2.soundName = UILocalNotificationDefaultSoundName;
        notif2.repeatInterval = NSDayCalendarUnit;
        NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Evening", @"key", nil];
        notif2.userInfo = infoDict2;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif2];
        [notif2 release];
    }
}