UILocalNotification - 当应用程序未运行时如何处理?

发布于 2024-12-25 18:08:24 字数 811 浏览 1 评论 0原文

我现在已经在两个不同的应用程序中实现了 UILocalNotifications。一个使用位置服务,这意味着如果它被杀死,它(通常)会重新启动(所以这不是一个大问题)。但是,在另一个应用程序中,我根据时间安排 UILocalNotification。在这种情况下,我遇到了一个大问题。

如果我安排通知,那么应用程序会被终止,内存不足,手机关闭然后再次打开,打开应用程序时我无法“自动”查看较旧的通知。

这是有效的工作流程(应用程序在后台运行):

  1. 启动应用程序;应用程序安排通知;关闭应用程序;应用程序现在在后台运行
  2. 接收本地通知;一开始忽略它;从顶部(状态栏)拉出下拉菜单;触摸通知以启动应用程序
  3. 结果:应用程序正确显示通知中的信息。

以下是不起作用的工作流程(应用程序不再在后台运行):

  1. 启动应用程序;应用程序安排通知;关闭应用程序; app现在正在后台运行
  2. 手动杀死app(模拟我的情况)
  3. 仍然收到本地通知;一开始忽略它;从顶部(状态栏)拉出下拉菜单;触摸通知以启动应用程序
  4. 结果:应用程序启动,但未调用 didReceiveLocalNotification 方法。用户认为该应用程序无法运行。
    注意:我什至无法手动强制显示这些信息,因为如果他们收到了多个通知,我无法判断他们触摸了哪一个来知道要显示哪一个。

当应用在后台运行(因此不运行didReceiveLocalNotification方法)时,有什么方法可以知道他们触及了哪个通知?

I have implemented UILocalNotifications in two different apps now. One uses Location Services, which means if it gets killed, it (usually) gets restarted (so it's not as big of an issue). However, in another app, I schedule a UILocalNotification based upon time. In this case, I'm having a major problem.

If I schedule a notification, then the app gets killed, pushed out of memory, phone is turned off then on again, I cannot view older notifications "automatically" when opening the app.

Here is the workflow that works (app running in the background):

  1. Launch app; app schedules notification; close app; app is now running in the background
  2. Receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
  3. Results: The app appropriately displays the information in the notification.

Here is the workflow that does not work (app no longer running in the background):

  1. Launch app; app schedules notification; close app; app is now running in the background
  2. Manually kill app (to simulate my situation)
  3. Still receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
  4. Results: The app launches, but the didReceiveLocalNotification method is not called. The user thinks the app doesn't work.
    Note: I can't even manually force the information, because if they have received more than one notification, I can't tell which one they touched on to know which one to display.

Is there any way to know which notification they touched on when the app is not running in the background (and thus does not run the didReceiveLocalNotification method)?

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

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

发布评论

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

评论(2

锦上情书 2025-01-01 18:08:24

在第二种情况下,用户已退出(或终止)应用程序,因此当用户触摸通知时,您的应用程序将启动。

这可以在下面提到的 appDelegate 的方法

didFinishLaunchingWithOptions 中处理,该方法在应用程序启动时调用,因此只需检查应用程序是否因用户触摸通知而实际启动(代码如下):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) 
    {
        NSLog(@"notification caused app to launch, alert body = %@", notification.alertBody);
        // do what ever action you want to do
        // you could just copy the code from "didReceiveLocalNotification" and paste it here
    }

    return YES;
}

In the 2nd case, the user has quit (or killed) the app, so when the user touches the notification your app would be launched.

This can be handled in the below mentioned appDelegate's method

didFinishLaunchingWithOptions is invoked when an app is launched, so just check if the app was actually launched because the user touched the notification (code below):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) 
    {
        NSLog(@"notification caused app to launch, alert body = %@", notification.alertBody);
        // do what ever action you want to do
        // you could just copy the code from "didReceiveLocalNotification" and paste it here
    }

    return YES;
}
智商已欠费 2025-01-01 18:08:24

@Ranjit,当用户点击应用程序图标时,这将起作用,因为这是一个应用程序委托方法。每次应用程序启动完成时都会接到电话。 (无法发表评论,因为我缺乏声誉......;))

@Ranjit, this will work when user tap the app icon because is an app delegate method. Get called every time the app finished launching. (Can´t comment because my lack of reputation... ;) )

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