我刚刚升级了注册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秒然后崩溃。