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