如果两个本地通知具有相同的触发时间并且应用程序位于后台,则 didReceive Local notification 仅被调用一次

2024-05-22

这很容易重现 - 看起来像是 iOS 的一个错误?如果它是一个功能,那么如何判断第二个通知是否已被接受?


更新:这不限于具有相同触发日期的两个或多个通知,它们可以有任何触发日期,但如果它们在应用程序处于后台时都过期,那么它仍然是相同的结果 - didReceiveLocalNotification 仅被调用一次。


如果两个本地通知被安排为具有相同的触发日期,并且应用程序位于后台(applicationDidEnterBackground:是最后一个被调用的方法),那么这是通知触发时发生的步骤序列:

1)向用户显示两个通知

2)用户点击第一个通知

3) applicationWillEnterForeground: 作为步骤 2 的结果被调用

4)didReceive本地通知:被调用

5) applicationDidBecomeActive: 未被调用,直到第 6 步为止不会再发生任何事情

6)用户点击第二条通知

7) 现在,第 6 步的结果是调用 applicationDidBecomeActive

8) 在第 6 步之后不会第二次调用 didReceiveLocalNotification

这样做的结果是应用程序不会收到任何表明用户接受了多个本地通知的指示。

这很容易重现 - 创建一个新项目,添加以下代码,运行然后在触发通知之前退出应用程序。

-

 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions");
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSDate* date = [[NSDate alloc] initWithTimeIntervalSinceNow:20];

    UILocalNotification *notif1 = [[UILocalNotification alloc] init];
    notif1.fireDate = date;
    notif1.timeZone = [NSTimeZone defaultTimeZone];
    notif1.alertBody = @"Notif1";

    UILocalNotification *notif2 = [[UILocalNotification alloc] init];
    notif2.fireDate = date;
    notif2.timeZone = [NSTimeZone defaultTimeZone];
    notif2.alertBody = @"Notif2";

    [[UIApplication sharedApplication] scheduleLocalNotification:notif1];
    [[UIApplication sharedApplication] scheduleLocalNotification:notif2];
    NSLog(@"Notifications scheduled");
    return YES;
}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"didReceiveLocalNotification: %@", notification.alertBody);
}

这是控制台输出:

2012-02-01 21:32:12.832NotificationsTest[13223:f803] didFinishLaunchingWithOptions 2012-02-01 21:32:15.755NotificationsTest[13223:f803] 安排的通知 2012-02-01 21:32:15.756NotificationsTest[1322 3:F803 ] applicationDidBecomeActive:2012-02-01 21:32:22.838NotificationsTest[13223:f803] applicationWillResignActive:2012-02-01 21:32:22.840NotificationsTest[13223:f803]applicationDidEnterBackground:

[显示两个通知,Notif2 位于 Notif1 之上]

[用户查看Notif2]

2012-02-01 21:33:34.177NotificationsTest[13223:f803] applicationWillEnterForeground: 2012-02-01 21:33:34.179NotificationsTest[13223:f803] didReceiveLocalNotification: Notif2

[用户浏览Notif1]

2012-02-01 21:33:58.278 通知测试 [13223:f803] applicationDidBecomeActive:

应用程序如何获取第二次通知已过期的信息及其内容?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果两个本地通知具有相同的触发时间并且应用程序位于后台,则 didReceive Local notification 仅被调用一次 的相关文章

随机推荐