[英]WatchOS App 4.0: How to schedule a local notification
我的應用程序處於后台或非活動模式,然后本地通知不起作 我從未收到過關於觀看的本地通知。
更新:少於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被鎖定。 當涉及通知時,它關於在何處提供該通知的偏好。
在模擬器上運行您的手表應用程序,從iPhone模擬器安排通知並鎖定iPhone模擬器屏幕,保持手表模擬器處於活動狀態,在這種情況下,當觸發通知時,它將在您的手表模擬器上傳送。 當您在實際設備上進行測試時也是如此 。 來源鏈接
當iPhone和手表都被鎖定時,首選是iphone。
UPDATE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.