iOS10 - userNotificationCenter didReceiveNotificationResponse以10秒延迟调用

时间:2017-03-22 12:04:45

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

我刚刚升级了注册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中用于深层链接。当我添加断点时,我发现这种情况发生了: 我正确收到了推送通知,我点击它,应用程序用户界面冻结了大约10秒钟。然后,最后调用didReceiveNotificationResponse并进行深层链接。

如何避免这种使应用程序冻结的巨大延迟?

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

编辑:这是我在xCode冻结时暂停主线程的截图: enter image description here

1 个答案:

答案 0 :(得分:0)

你的堆栈跟踪中有一些奇怪的东西。 semaphore_wait_trap semaphore_wait_slow 。尝试查看herehere以及here。我说猜测就是说你从一个错误的线程中调用了你的(void)registerForNotifications并导致了这个问题。

此外,我不明白为什么你的实施中有dispatch_async(dispatch_get_main_queue似乎是不必要的。尝试查看here

的答案