简体   繁体   English

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

[英]WatchOS App 4.0: How to schedule a local notification

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. 更新:少于3分钟安排本地通知它工作正常但超过3分钟它不起作用。 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. 感谢是否有任何建议或想法。

Make sure your iphone is locked. 确保你的iPhone被锁定。 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. 在模拟器上运行您的手表应用程序,从iPhone模拟器安排通知并锁定iPhone模拟器屏幕,保持手表模拟器处于活动状态,在这种情况下,当触发通知时,它将在您的手表模拟器上传送。 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. 当iPhone和手表都被锁定时,首选是iphone。

UPDATE UPDATE

Notification on Apple Watch Apple Watch上的通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM