【问题标题】:iOS10 - userNotificationCenter didReceiveNotificationResponse called with a 10sec delayiOS10 - 延迟 10 秒调用 userNotificationCenter didReceiveNotificationResponse
【发布时间】:2017-08-14 12:53:21
【问题描述】:

我刚刚升级了注册 iOS 10 的整个 iOS 推送通知,使用以下代码:

-(void)registerForNotifications{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        dispatch_async(dispatch_get_main_queue(), ^(void){
            if(!error){
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }
        });
    }];
}
else {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}

}

我的所有委托都设置在我的 AppDelegate 中。

编辑:我现在能够进一步确定问题所在。当应用在通知推送后返回前台时,委托:

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

仅在大约 10-15 秒后调用,而通常它显然是立即调用的。这怎么可能?

我现在正在使用 Localytics 测试推送通知并实施:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

在我的 AppDelegate 中用于深度链接。当我添加断点时,我看到发生了这种情况: 我正确收到推送通知,点击它,应用程序 UI 冻结了大约 10 秒。最后,调用 didReceiveNotificationResponse 并进行深度链接。

如何避免导致应用冻结的巨大延迟?

编辑:它甚至比我更糟糕。当我将 iPhone 连接到 xCode 并在手机上运行构建时,它会在工作前冻结十秒钟。然后,如果我只是运行完全相同的构建而不在 xCode 上运行它(所以没有断点),应用程序会冻结 10 秒然后崩溃。

编辑:这是我的主线程在 xCode 冻结时暂停时的屏幕截图:

【问题讨论】:

  • 如果没有深度链接检查一次,延迟是否存在?
  • 是的,即使没有深度链接,我仍然有延迟。 didReceiveNotificationResponse 无论是否存在深层链接都会被调用,因此问题会提前发生。
  • @bloemy :- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 中的代码需要 10 秒才能执行,还是需要时间来实现此方法?为什么不在此方法的第一行添加一个断点并检查是否需要 10 秒才能到达那里。
  • 是时候达到方法了。我的第一个断点位于方法名称本身。方法里面的代码是立即执行的,因为我调用了主线程。这似乎是一个线程问题......
  • 你提到了一个崩溃,你能补充更多细节吗?

标签: ios objective-c apple-push-notifications localytics


【解决方案1】:

您的堆栈跟踪中有一些奇怪的东西。 semaphore_wait_trapsemaphore_wait_slow。尝试查看hereherehere。话虽如此,我的猜测是您从错误的线程中调用了您的(void)registerForNotifications,这导致了这个问题。

另外,我不明白为什么您的实现中有dispatch_async(dispatch_get_main_queue似乎没有必要。试试看here的答案

【讨论】:

  • 嗨,肯定有一些奇怪的东西。我已经看到了所有这些线程,但还没有找到问题......奇怪的是,我只在第二次应用启动时调用 regsiterForNotifications,而不是在第一次启动时调用。 dispatch_asynch 确实在那里查看问题是否与主线程有关,但它没有帮助。
  • @bloemy “我只在第二次应用启动时调用 regsiterForNotifications” DidFinishLaunchingWithOptions 中打电话给registerForNotifications 吗?如果不是,那么您从哪里调用它,您应该能够关注 stackTrace 并找出从哪里/何时调用它
  • 不抱歉,我的意思是:我不会在第一次启动时从 AppDelegate 调用 registerForNotifications,因为我想自定义我的注册消息给用户。但是流程有效:它实际上可以与 Localytics 以外的其他框架一起使用,这真的很奇怪。
  • @bloemy 忘记本地化,只需编辑您的问题并显示所有相关代码。展示它是如何开始的,它是如何结束的
猜你喜欢
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2019-02-04
  • 2012-01-08
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多