我如何了解通知导致应用程序处于什么状态?

发布于 2024-11-30 13:34:54 字数 277 浏览 2 评论 0原文

我设置了一个 UILocalNotification,据我所知,我有 5 种不同的场景:

  1. 应用程序未运行,用户选择查看通知,因此它启动应用程序。
  2. 应用程序未运行,用户选择关闭通知,然后稍后打开应用程序。
  3. 该应用程序在后台运行,用户选择查看通知,因此会将应用程序带到前台。
  4. 该应用程序在后台运行,用户选择关闭通知,然后打开应用程序,稍后将其带到前台。
  5. 该应用程序正在前台运行。

我该如何处理这 5 种不同的情况?

I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:

  1. The app is not running, the user chooses to view the notification, so it launches the app.
  2. The app is not running, the user chooses to close the notification, then opens the app at a later date.
  3. The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
  4. The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
  5. The app is running in the foreground.

How do i deal with these 5 different scenarios?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍白女子 2024-12-07 13:34:54
  1. 将代码放入 application:didFinishLaunchingWithOptions: 中。在
    使用 NSDictionary 操作您将找到有关
    通知。

  2. 您可以再次检查 application:didFinishLaunchingWithOptions: 如果本地
    通知仍处于活动状态并采取适当的操作。

  3. 将您的代码放入 applicationWillEnterForeground:

  4. 同样的位置,只需检查是否有活动的本地通知。

  5. 在这里您可以签入application:didReceiveLocalNotification:并通知或不通知用户。

  1. Put your code into application:didFinishLaunchingWithOptions:. In
    the actions NSDictionary you will find the information about the
    notification.

  2. You can again check in application:didFinishLaunchingWithOptions: if the local
    notification is still active and take appropriate action.

  3. Put your code into applicationWillEnterForeground:

  4. Again the same spot, just check if there are active local notifications.

  5. Here you can check in application:didReceiveLocalNotification: and either notify the user or not.

随波逐流 2024-12-07 13:34:54

不太确定您在追求什么,但以下内容可能会回答您的问题。

来自文档

当系统发送本地通知时,可能会发生多种情况,具体取决于应用程序状态和通知类型。如果应用程序不是最前面且不可见,则系统会显示警报消息、标记应用程序并播放声音(无论通知中指定什么内容)。如果通知是警报,并且用户点击操作按钮(或者,如果设备已锁定,则拖动打开操作滑块),则会启动应用程序。在 application:didFinishLaunchingWithOptions: 方法中,应用程序委托可以使用 UIApplicationLaunchOptionsLocalNotificationKey 键从传入的选项字典中获取 UILocalNotification 对象。委托可以检查通知的属性,如果通知在其 userInfo 字典中包含自定义数据,则它可以访问该数据并进行相应的处理。另一方面,如果本地通知仅标记应用程序图标,并且用户响应启动应用程序,则将调用 application:didFinishLaunchingWithOptions: 方法,但选项字典中不包含 UILocalNotification 对象。

如果系统发送通知时应用程序位于最前面并且可见,则不会显示任何警报,不会标记任何图标,并且不会播放任何声音。但是,如果应用程序委托实现了 application:didReceiveLocalNotification: ,则会调用它。 UILocalNotification 实例被传递到此方法中,委托可以检查其属性或访问 userInfo 字典中的任何自定义数据。

Not exactly sure what you're after, but the following might answer your question.

From the documentation:

When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.

If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文