2

正如我在指南中看到的那样,通知会自动显示。但是..不适合我。问题是为什么?当用户点击按钮时,我的 iOS 应用程序会在 4 秒后发送通知:

@IBAction func stopTimerPress(sender: AnyObject) {
        print("Stop timer!")
        notification.alertBody = "STOP!"
        self.timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "fire", userInfo: nil, repeats: true)
}

然后我希望(如果 iOS 中的应用程序在后台)我的 Apple Watch 显示静态通知。但它没有发生。我能做些什么?我取消了注释:

override func didReceiveLocalNotification(localNotification: UILocalNotification, withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
        print(localNotification.alertBody)
        completionHandler(.Default)
    }

在 NotificationController 中,但这仍然不起作用。有人能帮我吗?

4

2 回答 2

1

您是否请求用户允许查看 iOS 应用程序上的通知?如果 iPhone 屏幕关闭(不仅仅是后台的应用程序),那么它应该将通知转发到 Apple Watch。

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

此外,如果 Apple Watch 应用程序在前台并且在通知到达时屏幕处于打开状态,则它不会显示在 UI 中,而是会调用 didReceiveLocalNotification 方法。如果屏幕关闭,则不会调用此方法,但通知将显示为通常的通知。

于 2015-11-09T15:44:44.453 回答
0

当您在后台时,您的代码没有运行。但是,如果您在 Xcode 中单击您的项目,然后转到“功能”,然后进入“后台模式”并单击“远程通知”。然后您的应用程序将在只有 Apple 知道的时间被唤醒,然后您的代码将在持续时间不超过 30 秒的情况下运行。您可以使用 Debug 菜单中的 Xcode 菜单项“Simulate Background Fetch”在调试您的应用程序时发送一条获取消息,以进行测试。

请注意,这可能会或可能不会在 4 秒内运行您的代码 - 因为 Apple 决定何时唤醒您的后台应用程序。我看到它每 5 分钟醒来一次,甚至在几个小时后醒来。

于 2015-11-09T15:18:59.417 回答