Search code examples
iosuilocalnotificationwatchkit

About local notifications on Apple watch app


I have managed scheduling local notification on ios app and have tested them on static notifications interface on apple watch. But, there is a glitch (maybe not?) with that, static local notification interface is being displayed even when the watch app is still running i.e. on the foreground (iphone app is backgrounded and iphone is locked). Also when watch app is active, iphone app is backgrounded, and iphone is NOT locked, iphone is displaying the notification. This is really weird to me.

I'm scheduling local notification as below:

NSString *textToShow = nil;
        NSSet *categories = nil;
        textToShow = text;
        UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
        notificationAction1.identifier = @"openWKApp";
        notificationAction1.title = localStr(@"View");
        notificationAction1.activationMode = UIUserNotificationActivationModeForeground;
        notificationAction1.destructive = NO;
        notificationAction1.authenticationRequired = NO;

        UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
        notificationCategory.identifier = @"openWKApp";
        [notificationCategory setActions:@[notificationAction1] forContext:UIUserNotificationActionContextDefault];
        [notificationCategory setActions:@[notificationAction1] forContext:UIUserNotificationActionContextMinimal];
        categories = [NSSet setWithObjects:notificationCategory, nil];

        UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = date;

        localNotification.alertBody = @"Notification body";

        localNotification.applicationIconBadgeNumber = 1;
        localNotification.userInfo = info;
        localNotification.category = @"openWKApp";

        localNotification.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

If anybody has faced the same and fixed or found the way around, please, help!


Solution

  • Ended up canceling notifications on willActivate, and re-scheduling on didDeactivate of watchkit. Still, it's really weird to receive notification when watchkit app is running.