WatchOS App 4.0:如何安排本地通知

时间:2017-11-06 10:09:08

标签: ios objective-c watch localnotification

我的应用程序处于后台或非活动模式,然后本地通知不起作用。我从未收到过关于观看的本地通知。

更新:少于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);
    }
}];

欣赏是否有任何建议或想法。

1 个答案:

答案 0 :(得分:1)

确保您的iphone已锁定。当涉及通知时,它关于在何处提供该通知的偏好。

在模拟器上运行您的手表应用程序,从iPhone模拟器安排通知并锁定iPhone模拟器屏幕,保持手表模拟器处于活动状态,在这种情况下,当触发通知时,它将在您的手表模拟器上传送。您将在实际设备上进行测试的情况也是如此。 Source Link

当iphone和手表都被锁定时,首选是iphone。

更新

Notification on Apple Watch