ios 检查显示的本地通知

标签 ios uilocalnotification

假设我安排了 3 个本地通知,当用户打开应用程序时,三个通知具有以下状态:

a) not displayed yet
b) displayed
c) displayed and user taps on this notification and come back to app.

我如何检测每一个?

最佳答案

iOS 10 引入了 UIUserNotificationCenter ,并弃用了通过 UIApplication 安排和发送本地通知, 因此根据您使用的方法会有一些差异。已弃用的方法在 iOS 10 上仍然有效,因此如果您的目标是早于 iOS10 的 iOS,那么您可以继续使用这些方法,但您会收到弃用警告。

对于案例a,通知尚未送达,可通过调用getPendingNotificationRequests 获得此通知的详细信息。在 UIUserNotificationCenter 的实例上(iOS 10) 或通过访问 scheduledLocalNotifications您应用的属性 UIApplication实例(iOS 10 之前)

对于案例 b,已发送但用户未与之交互的通知,没有可用信息。

对于案例 c,用户点击启动应用程序的通知,它取决于应用程序的状态以及用户与通知交互的方式。

  • 如果应用程序未在前台运行或挂起,则会启动应用程序并将通知负载传送到 application:didFinishLaunchingWithOptions:通过 localNotification在选项字典中键入
  • 如果应用程序被暂停,则通知将发送至 didReceiveLocalNotification委托(delegate)方法被调用。
  • 如果通知有自定义操作并且用户点击其中之一,则 application:handleActionWithIdentifier:forLocalNotification:completionHandler:应用委托(delegate)方法被调用

关于ios 检查显示的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39759916/

相关文章:

ios - 移动行在索引路径 : how to delete section once last row is moved to another section

iphone - 如何在不卡住 UI 的情况下安排多个本地通知?

ios - 如何将我的应用程序作为模块包含在 Swift Playground 中?

ios - 在不提交的情况下更改对象的属性

ios - 调用 NSLayoutConstraint.activateConstraints 会使约束无效吗?

ios - 当从 Interface Builder 设置 UIImageView 的图像时,该图像是如何加载的?

ios - 查找应用已设置的本地通知列表

ios - 应用程序关闭时从本地通知中选择表格 View 单元格

swift - iOS8 - UILocalNotification,稍后从代码中检查权限

ios - iOS 的 UILocalNotification 中不显示操作和类别