アプリケーションにローカル通知を実装しましたが、以下のシナリオで問題が発生しました:
シナリオ:アプリが起動され、現在の日付から 1 週間後にログイン画面に表示される位置通知が作成されます。資格情報を使用してログインし、ログイン後にデバイスのホーム ボタン (中央下部の物理キー) をダブルタップして、最近使用したアプリケーション バーで、アプリケーション アイコンをタップ アンド ホールドして、アプリケーションを強制終了できるようにします。アプリケーションを強制終了した後、1 週間待たずに通知ポップアップが表示されます。
以下はコードです:
- (void)applicationWillTerminate:(UIApplication *)application
{
if (iPhoneClientConfig::getInstance()->getReminder() == RS_NONE) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
else {
int daysToAdd;
NSString *alertText;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (iPhoneClientConfig::getInstance()->getReminder() == RS_MONTH) {
daysToAdd = 60*60*24*30;
alertText = [Localization getLanguage:@"reminder_message_month"];
localNotification.repeatInterval = NSMonthCalendarUnit;
[self generateAlertNotifications:daysToAdd withtext:alertText ];
} else if (iPhoneClientConfig::getInstance()->getReminder() == RS_DAY) {
daysToAdd = 60*60*24;
alertText = [Localization getLanguage:@"reminder_message_day"];
localNotification.repeatInterval = NSDayCalendarUnit;
} else if (iPhoneClientConfig::getInstance()->getReminder() == RS_HOUR) {
daysToAdd = 60*60;
alertText = [Localization getLanguage:@"reminder_message_hour"];
localNotification.repeatInterval = NSHourCalendarUnit;
} else {
if (iPhoneClientConfig::getInstance()->getReminder() != RS_WEEK) {
LOG.error("%s: Unexpetected reminder repeat interval in configuration, fall back to week",__FUNCTION__);
}
daysToAdd = 60*60*24*7;
alertText = [Localization getLanguage:@"reminder_message_week"];
localNotification.repeatInterval = NSWeekCalendarUnit;
[self generateAlertNotifications:daysToAdd withtext:alertText];
}
NSDate *today = [NSDate date];
NSDate *newDate = [today addTimeInterval:daysToAdd];
NSLog(@"newDate:%@",newDate);
// Set up the fire time
localNotification.fireDate = newDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// Notification details and Set the action button
localNotification.alertBody = alertText;
localNotification.alertAction = [Localization getLanguage:@"local_notif_alert"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
//Schedule LocalNotification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"local_notif_alert:%@",localNotification);
[localNotification release];
}
}
以下のように、コンソールに発火日とローカル通知があります。
Date:2012-09-01 10:49:08 +0000
local_notif_alert:<UIConcreteLocalNotification: 0xd006d20>{fire date = Saturday, September 1, 2012 4:19:08 PM India Standard Time, time zone = Asia/Kolkata (IST) offset 19800, repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, September 1, 2012 4:19:08 PM India Standard Time}