本地通知问题苹果手机



我想发出本地通知,每天通知用户 5 次,每天重复,我将它们放在一个可变数组中,该数组的对象是"hh:mm",GMT+3 城镇的小时和分钟是固定的,所以我获取当前日期并找到间隔,然后为通知创建一个日期这就是我实施的方法。-首先应用时区,-秒,如果时间在当前时间之前,所以让它在第二天。-第三设置该日期的本地通知。请帮助我

一段代码

通过使用此示例代码,我已经安排了 2 个通知,一个在早上 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];
    }
}

最新更新