トップ «前の日記(2012-02-02) 最新 次の日記(2012-02-04)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-02-03 [iOS]Local Notification

Notificationには、LocalとPushの二種類があって、前者は発生源が手元のアプリケーション、後者は発生源が外部という事のようだ。

Local Notificationの登録は、通知する日時を指定する方法と、直ぐに通知する方法の二通りがある。

通知する日時を指定する手順は以下のとおり。

- (IBAction)scheduleNotification:(id)sender
{
    DBGMSG(@"%s", __func__);
    NSDate  *today = [NSDate date];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = [today dateByAddingTimeInterval:10];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = [NSString stringWithString:NSLocalizedString(@"Local Notify", nil)];
    localNotif.alertAction = NSLocalizedString(@"local notify", nil);
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber + 1;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"local notify" forKey:@"Key"];
    localNotif.userInfo = infoDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

UILocalNotificationのプロパティfireDateに、通知する日時を設定する。上記の例は、現在の時刻の10秒後だ。

UILocalNotificationのインスタンスをUIApplicationのscheduleLocalNotification:メソッドに設定すると、指定された日時に通知される。

直ぐに通知させたい場合は、presentLocalNotificationNow:メソッドを呼ぶ。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    DBGMSG(@"Application entered background state.");
    NSAssert(self.bgTask == UIBackgroundTaskInvalid, nil);
    self.bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self.bgTask];
            self.bgTask = UIBackgroundTaskInvalid;
        });
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        while ([application backgroundTimeRemaining] > 1.0) {
            UILocalNotification *localNotif = [[UILocalNotification alloc] init];
            if (localNotif) {
                localNotif.alertBody = [NSString stringWithString:NSLocalizedString(@"background", nil)];
                localNotif.alertAction = NSLocalizedString(@"action", nil);
                localNotif.soundName = UILocalNotificationDefaultSoundName;
                localNotif.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber + 1;
                [application presentLocalNotificationNow:localNotif];
                break;
            }
        }
        [application endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
    });
}

通知を受けてアプリケーションを呼び出す場合は、アプリケーション側で準備が必要となる。

アプリケーションの起動されていない場合は、アプリケーションのデリゲートのdidFinishLaunchingWithOptions:メソッドで通知からの呼び出しかどうかの判断を行う。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        NSString *value = [localNotif.userInfo objectForKey:@"Key"];
        DBGMSG(@"%s, Notify: %@", __func__, value);
        application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber - 1;
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

起動済みの場合は、didReceiveLocalNotification:メソッドが呼ばれる。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    NSString *value = [notif.userInfo objectForKey:@"Key"];
    NSLog(@"%s, Notify: %@", __func__, value);
    app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber - 1;
}

_ 関連情報

Local and Push Notification Programming Guide
iOS Developer Libraryの情報です。

トップ «前の日記(2012-02-02) 最新 次の日記(2012-02-04)» 編集