2

我刚刚使用以下代码升级了注册 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 冻结时暂停时的主线程屏幕截图: 在此处输入图像描述

4

1 回答 1

0

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

此外,我不明白为什么您dispatch_async(dispatch_get_main_queue的实施中有一个。似乎没有必要。尝试从这里查看答案

于 2017-03-23T15:03:39.143 回答