本地通知并不总是发送

时间:2015-04-23 01:05:22

标签: ios objective-c xcode apple-push-notifications uilocalnotification

我设置本地通知以在每天下午7:45:01开火,但当时没有每天开火。< / p>

知道我在这里失踪的是什么吗?我已经尝试过编辑代码,但我仍然不知道自己错过了什么。

将根据需要发布任何额外的代码,谢谢!

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

    return YES;
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
    comp.hour = 19; // 19 = 7PM
    comp.minute = 45; // 7:45 PM
    comp.second = 01; // 7:45:01 PM

    localNotification.fireDate = [cal dateFromComponents:comp];
    localNotification.alertBody = @"Local Notification in iOS8";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

1 个答案:

答案 0 :(得分:1)

  

我设置了一个本地通知,每天下午7:45:01开火

不,你没有。您设置本地通知以启动一次。您没有设置任何repeatInterval,因此这不是重复通知。它会发射一次而那就是那个。