WatchOS App 4.0: How to schedule a local notification

寵の児 提交于 2020-01-02 02:53:26

问题


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.


回答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



来源:https://stackoverflow.com/questions/47134384/watchos-app-4-0-how-to-schedule-a-local-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!