UILocalNotification fireDate没有按预期工作(至少我预期的那样)

时间:2013-12-24 23:59:54

标签: ios nsdate uilocalnotification

您好我试图在24小时后将本地通知设置为开机但仅进行测试我已将其降至30秒。

问题是本地通知仅触发ONCE(30秒后)然后......没有

这是我的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"myTipOfTheDay" ofType:@"plist"];

    NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSMutableArray *plistArray = plistDict[@"tipOfTheDay"];
    int randTip = arc4random() % plistArray.count;

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:30];  //60*60*24 for 24 hours
 **EDIT:**   //notification.repeatInterval = NSMinuteCalendarUnit; This only repeats the SAME message every min and does not fire up a random string from my plist....
    notification.alertBody = plistArray[randTip];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
 //cancel notifications if app is active
}

1 个答案:

答案 0 :(得分:0)

显然,每次调用repeatInterval时,我们都无法按需更改alertBody。 太错了......

无论如何这里是我现在使用的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"Hey, I've got a tip for you...";
    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24]; 
    notification.repeatInterval = NSDayCalendarUnit; //NSMinuteCalendarUnit | NSDayCalendarUnit
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1;

}


 - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];



     if (application.applicationIconBadgeNumber > 0) { //check if user has been notified... 
even if user cleared the notification, an alert will still show with the Tip of The Day...

    NSString *path = [[NSBundle mainBundle] pathForResource:
                                  @"myTipOfTheDay" ofType:@"plist"];

                NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
                NSMutableArray *plistArray = plistDict[@"randTip"];
                int randV = arc4random() % plistArray.count; //random selection of string

            UIAlertView *tipOfTheDaAlert = [[UIAlertView alloc] initWithTitle:@"Did you know that..."
                                                                      message:plistArray[tipOfDay]
                                                                     delegate:self
                                                            cancelButtonTitle:@"Dismiss"
                                                            otherButtonTitles:nil];

            [tipOfTheDaAlert show];

            application.applicationIconBadgeNumber = 0; //reset the icon badge to zero
        }
    }