我的应用程序在后台或非事件模式下本地通知不起作用。我从来没有在 watch 上收到本地通知。
更新:不到 3 分钟安排一个本地通知它工作正常但超过 3 分钟它就不起作用。那么如何解决这个问题呢?
根据我的理解,我的代码如下。
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);
}
}];
如果有任何建议或想法,我们将不胜感激。
最佳答案
确保您的 iPhone 已锁定。当涉及到通知时,它是关于将通知发送到哪里的偏好。
在模拟器上运行您的 watch 应用程序,从 iPhone 模拟器安排通知并锁定 iPhone 模拟器屏幕,保持 watch 模拟器处于事件状态,在这种情况下,当通知被触发时,它将传递到您的 watch 模拟器。当您在实际设备上进行测试时也是如此。 Source Link
当 iphone 和 watch 都被锁定时,首选是 iphone。
更新
关于ios - WatchOS 应用程序 4.0 : How to schedule a local notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47134384/