LoginSignup
81

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-09-11

ハマったのでメモ。

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

    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 周りはいろいろ面白そうなのであとで調べる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81