简体   繁体   English

已安排iOS UILocalNotification,但未触发

[英]iOS UILocalNotification scheduled, but not fired

I'm making an application where I want local notifications. 我正在制作需要本地通知的应用程序。 And something strange is happening. 而且正在发生奇怪的事情。 I schedule the notification: 我安排了通知:

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 ))
}

I print in how much seconds i am supposed to get an notification. 我要打印多少秒钟才能收到通知。 I go to background mode, and keep watching when I will get an notification. 我进入后台模式,一直在等待何时收到通知。 when the time has passed, I get no notification, even though I am sure I am in background mode. 时间过去后,即使我确定自己处于后台模式,也不会收到任何通知。 (within Xcode I look at the debug navigator > Energy Impact, and it is saying I am in background). (在Xcode中,我查看调试导航器> Energy Impact,它表示我处于后台)。

When I restart my phone, and run the application, it does show the notification. 当我重新启动手机并运行该应用程序时,它会显示通知。 Everything works perfect. 一切正常。 And then later, after some more testing and using the application, my notifications stop working again (even though the notifications are still scheduled. I am following all the scheduled notifications with: 然后,在经过更多测试并使用了该应用程序之后,我的通知再次停止工作(即使通知仍在计划中。我会使用以下所有计划的通知:

        UIApplication.sharedApplication().scheduledLocalNotifications

I am still new with Swift, and I have no idea why this is happening. 我对Swift还是很陌生,我也不知道为什么会这样。 It's making me crazy not knowing why my notification is not firing, even though it is scheduled and everything... 这让我发疯,即使通知已安排好一切,也不知道为什么我的通知没有被触发...

Could anyone help me? 有人可以帮我吗? If you need more information, please ask. 如果您需要更多信息,请询问。

  • Edit: 编辑:
    • I did set the permissions 我确实设置了权限
    • The notifications are set in the AppDelegate 通知在AppDelegate中设置
    • The departure time definitely is right 出发时间绝对正确

I made a mistake by not adding an unique ID to the notification (notification.userInfo was not always unique), and overwrites the previous scheduled notification. 我没有在通知中添加唯一的ID(notification.userInfo并非始终是唯一的)而犯了一个错误,并覆盖了之前的预定通知。 Thats why it something does work and sometimes doesn't. 这就是为什么某些东西行之有效,有时却行不通的原因。

Fixed the problem by making it unique. 通过使其唯一来解决该问题。 Thanks 谢谢

Since iOS 8.0, You have to ask for permission for local notifications as remote notifications: 从iOS 8.0开始,您必须请求将本地通知作为远程通知的权限:

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

See more details in Location Notifications 位置通知中查看更多详细信息

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

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