繁体   English   中英

已安排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中,我查看调试导航器> Energy Impact,它表示我处于后台)。

当我重新启动手机并运行该应用程序时,它会显示通知。 一切正常。 然后,在经过更多测试并使用了该应用程序之后,我的通知再次停止工作(即使通知仍在计划中。我会使用以下所有计划的通知:

        UIApplication.sharedApplication().scheduledLocalNotifications

我对Swift还是很陌生,我也不知道为什么会这样。 这让我发疯,即使通知已安排好一切,也不知道为什么我的通知没有被触发...

有人可以帮我吗? 如果您需要更多信息,请询问。

  • 编辑:
    • 我确实设置了权限
    • 通知在AppDelegate中设置
    • 出发时间绝对正确

我没有在通知中添加唯一的ID(notification.userInfo并非始终是唯一的)而犯了一个错误,并覆盖了之前的预定通知。 这就是为什么某些东西行之有效,有时却行不通的原因。

通过使其唯一来解决该问题。 谢谢

从iOS 8.0开始,您必须请求将本地通知作为远程通知的权限:

let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)   

位置通知中查看更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM