Objective-C
iPhone
iOS
iOS8

iOS8 からローカル通知が保存されなくなった?

More than 3 years have passed since last update.

ハマったのでメモ。

単純なローカル通知を追加。

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

notify.fireDate = [NSDate dateWithTimeIntervalSinceNow:100];
notify.alertBody = @"test";
[[UIApplication sharedApplication] scheduleLocalNotification:notify];

NSLog(@"notifications: %@", application.scheduledLocalNotifications);

もちろん iOS 7 だとうまくいく。

notifications: (

"<UIConcreteLocalNotification: 0x7fb6d2105e80>{fire date = Thursday, September 11, 2014 at 8:02:10 PM Japan Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, September 11, 2014 at 8:02:10 PM Japan Standard Time, user info = (null)}"
)

iOS8 だと変なエラー出て application.scheduledLocalNotifications が空っぽ。

Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7f8936b00a90>{fire date = 2014年9月11日木曜日 19時59分27秒 日本標準時, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = 2014年9月11日木曜日 19時59分27秒 日本標準時, user info = (null)} with an alert but haven't received permission from the user to display alerts

notifications: (
)

パーミッションがうんたらかんたら。

with an alert but haven't received permission from the user to display alerts

ぐぐったらあった。

■ scheduleLocalNotification doesn´t work - Stack Overflow

http://stackoverflow.com/questions/12277334/schedulelocalnotification-doesn%C2%B4t-work

これで解決。

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

{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
return YES;
}

リモート通知同様、ローカル通知でもちゃんと初期化しろってことらしい。

おかげでローカル通知でも起動時にダイアログ出るようになった。

通知ダイアログ

iOS8 の User Notifications 周りはいろいろ面白そうなのであとで調べる。