WatchOS App 4.0: How to schedule a local notification
Asked Answered
G

1

7

My application in background or inactive mode then local notification not work. I have never receive local notification on watch.

Update: less then 3 minutes schedule a local notification it's work fine but more then 3 minutes it's not work. so how to resolve this issues?

As per my understanding My code is as follows.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

// Objective-C
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = [NSString localizedUserNotificationStringForKey:@"Remider!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Your watch is out of range" arguments:nil];
content.sound = [UNNotificationSound defaultSound];

// Time
// 15 min
double timer = 15*60;
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time
                                                                                                repeats:NO];
// Actions
UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Track"
                                                                          title:@"Track" options:UNNotificationActionOptionForeground];

// Objective-C
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory"
                                                                          actions:@[snoozeAction] intentIdentifiers:@[]
                                                                          options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObject:category];

// Objective-C
[center setNotificationCategories:categories];

// Objective-C
content.categoryIdentifier = @"UYLReminderCategory";

NSString *identifier = [self stringUUID];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                      content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Something went wrong: %@",error);
    }
}];

Appreciate if any suggestion or idea.

Greenheart answered 6/11, 2017 at 10:9 Comment(0)
W
1

Make sure your iphone is locked. When it comes to notification, its about preference where to deliver that notification.

Run your watch app on simulator, from iPhone simulator schedule the notification and lock the iPhone simulator screen, keep the watch simulator active, in that case when notification is triggered , it will be delivered on your watch simulator. Same will be the case when you will test on actual devices. Source Link

And when both iphone and watch is locked, preference is iphone.

UPDATE

Notification on Apple Watch

Waler answered 21/11, 2017 at 7:12 Comment(7)
I know this. i was tested on actual device and i did not receive schedule a local notification on watch device.Greenheart
i tested with 10 minutes interval and it worked. Make sure notification is being scheduled. If it is , then it must have delivered somewhere, if not on watch then it must be iphone.Waler
I appreciate your work but i want this notification on watch app. any suggestion or idea for watch application?Greenheart
As i mentioned earlier, its about preference where to deliver notification and OS handles it. Details are mentioned in this link support.apple.com/en-us/HT204791. Try the customise notification option in my watch app.Waler
I like your comment but My watch application in background or inactive mode then get local notification. also my devices are disconnected with my watch and then need notification so above link not useful for me. need more help if you know.Greenheart
Its clearly mentioned in there that "When your devices disconnect, your notifications go to your iPhone instead of your Apple Watch". If you want notification on watch even your devices are disconnected then its not possible. What you want to achieve is currently the limitation in apple watch case.Waler
Yes i know apple limitation! but my client want this notification when iPhone and iWatch is pair but disconnected status then notification fire on watch app.Greenheart

© 2022 - 2024 — McMap. All rights reserved.