iOS 已安排 UILocalNotification,但未触发
iOS UILocalNotification scheduled, but not fired
我正在制作一个需要本地通知的应用程序。一些奇怪的事情正在发生。我安排通知:
static func setNotification(body: String, departure: Double, notification_info: Dictionary<String, String> )
{
let notification = UILocalNotification()
notification.alertBody = body
notification.fireDate = NSDate(timeIntervalSinceNow: departure)
notification.alertAction = "ShowDetails"
notification.userInfo = notification_info
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print("notification over: " + String(departure))
print("-------" + String( notification.fireDate ))
}
我打印我应该在多少秒内收到通知。
我进入后台模式,并继续观察何时会收到通知。时间过去后,即使我确定自己处于后台模式,我也不会收到任何通知。 (在 Xcode 我查看调试导航器 > 能源影响,它说我在后台)。
当我重新启动 phone 和 运行 应用程序时,它会显示通知。一切都很完美。然后,经过更多测试和使用该应用程序后,我的通知再次停止工作(即使通知仍在安排中。我正在关注所有安排好的通知:
UIApplication.sharedApplication().scheduledLocalNotifications
我对 Swift 还是个新手,我不知道为什么会这样。不知道为什么我的通知没有触发,这让我发疯了,即使它是预定的并且一切......
有人能帮帮我吗?如果您需要更多信息,请询问。
- 编辑:
- 我确实设置了权限
- 通知在AppDelegate中设置
- 出发时间绝对正确
从 iOS 8.0 开始,您必须请求本地通知作为远程通知的权限:
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
我犯了一个错误,没有在通知中添加唯一 ID(notification.userInfo 并不总是唯一的),并覆盖了之前预定的通知。这就是为什么有些东西会起作用,有时却不起作用。
已通过将问题设为唯一来解决问题。
谢谢
我正在制作一个需要本地通知的应用程序。一些奇怪的事情正在发生。我安排通知:
static func setNotification(body: String, departure: Double, notification_info: Dictionary<String, String> )
{
let notification = UILocalNotification()
notification.alertBody = body
notification.fireDate = NSDate(timeIntervalSinceNow: departure)
notification.alertAction = "ShowDetails"
notification.userInfo = notification_info
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print("notification over: " + String(departure))
print("-------" + String( notification.fireDate ))
}
我打印我应该在多少秒内收到通知。 我进入后台模式,并继续观察何时会收到通知。时间过去后,即使我确定自己处于后台模式,我也不会收到任何通知。 (在 Xcode 我查看调试导航器 > 能源影响,它说我在后台)。
当我重新启动 phone 和 运行 应用程序时,它会显示通知。一切都很完美。然后,经过更多测试和使用该应用程序后,我的通知再次停止工作(即使通知仍在安排中。我正在关注所有安排好的通知:
UIApplication.sharedApplication().scheduledLocalNotifications
我对 Swift 还是个新手,我不知道为什么会这样。不知道为什么我的通知没有触发,这让我发疯了,即使它是预定的并且一切......
有人能帮帮我吗?如果您需要更多信息,请询问。
- 编辑:
- 我确实设置了权限
- 通知在AppDelegate中设置
- 出发时间绝对正确
从 iOS 8.0 开始,您必须请求本地通知作为远程通知的权限:
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
我犯了一个错误,没有在通知中添加唯一 ID(notification.userInfo 并不总是唯一的),并覆盖了之前预定的通知。这就是为什么有些东西会起作用,有时却不起作用。
已通过将问题设为唯一来解决问题。 谢谢