【问题标题】:WatchOS App 4.0: How to schedule a local notificationWatchOS App 4.0:如何安排本地通知
【发布时间】:2018-04-18 11:09:26
【问题描述】:

我的应用程序处于后台或非活动模式,然后本地通知不起作用。我从来没有在手表上收到本地通知。

更新: 不到 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);
    }
}];

如果有任何建议或想法,请欣赏。

【问题讨论】:

    标签: ios objective-c watch localnotification


    【解决方案1】:

    确保您的 iPhone 已锁定。当涉及到通知时,它与发送通知的首选位置有关。

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

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

    更新

    Notification on Apple Watch

    【讨论】:

    • 我知道这一点。我在实际设备上进行了测试,但我没有在手表设备上收到安排本地通知。
    • 我以 10 分钟的间隔进行了测试,并且成功了。确保正在安排通知。如果是,那么它一定是在某个地方交付的,如果不是在手表上,那么它一定是 iphone。
    • 感谢您的工作,但我希望在手表应用上收到此通知。对手表应用有什么建议或想法吗?
    • 正如我之前提到的,它是关于在哪里传递通知和操作系统处理它的偏好。此链接support.apple.com/en-us/HT204791 中提到了详细信息。在我的手表应用中尝试自定义通知选项。
    • 我喜欢你的评论,但我的手表应用程序在后台或非活动模式下会收到本地通知。我的设备也与我的手表断开连接,然后需要通知,所以上面的链接对我没有用。如果您知道,需要更多帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多