ios - WatchOS 应用程序 4.0 : How to schedule a local notification

标签 ios objective-c watch localnotification

我的应用程序在后台或非事件模式下本地通知不起作用。我从来没有在 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。

更新

Notification on Apple Watch

关于ios - WatchOS 应用程序 4.0 : How to schedule a local notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47134384/

相关文章:

python - 当 zookeeper 的状态自动改变时,Watches 和 Ephemeral 节点不工作?

iphone - 使用 XCode 5 的 iPhone 弹出窗口 View

ios - 有没有办法报告 Apple 文档中的错误?

ios - queueReusableCellWithReuseIdentifier 在 Xcode 中做什么?

iphone - 指向倾斜方向 - iPhone

linux - grunt watch/sass 无法正常工作

ios - SpriteKit : How to animate drawing of a rounded rectangle

iphone - UITextField 不显示键盘

ios - 开发人员无需使用 Xcode 7 进行代码签名

AngularJS $watch 在文本输入中的每个按键上都会被调用