问题描述
我的应用程序处于后台或非活动模式,然后本地通知不起作用。我从未收到过关于观看的本地通知。
My application in background or inactive mode then local notification not work. I have never receive local notification on watch.
更新:少于3分钟安排本地通知它工作正常但超过3分钟它不起作用。那么如何解决这个问题?
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);
}
}];
感谢任何建议或想法。
推荐答案
确保您的iphone已锁定。当涉及通知时,它关于在何处提供该通知的偏好。
Make sure your iphone is locked. When it comes to notification, its about preference where to deliver that notification.
在模拟器上运行您的手表应用程序,从iPhone模拟器安排通知并锁定iPhone模拟器屏幕,保持手表模拟器处于活动状态,在这种情况下通知是触发后,它将在您的手表模拟器上发布。当您在实际设备上进行测试时也是如此。
当iphone和手表都被锁定时,首选是iphone。
And when both iphone and watch is locked, preference is iphone.
UPDATE
这篇关于WatchOS App 4.0:如何安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!