在IOS上,我们注册了远程通知,并观察了didRegisterForRemoteNotificationsWithDeviceToken回调以获取设备令牌。
但是,对于Rich通知,使用新的UNUserNotificationCenter代码,我们将带有完成处理程序的requestAuthorizationWithOptions使用。完成处理程序调用registerForRemoteNotifications,但这不在主线程上,并且XCode抱怨它必须存在。
然后我们使用dispatch_async在主线程上调用它,如下所示,但如果这样做,则不会得到didRegisterForRemoteNotificationsWithDeviceToken或didFailToRegisterForRemoteNotificationsWithError回调。
我们应该忽略XCode警告并且不使用dispatch_async,还是有其他原因导致我们不获取回调?
谢谢
肖恩
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max)
{
//code for old IOS
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
Log( "PUSHNOTIF Called Register" );
});
}
else
{
Log( "PUSHNOTIF Error from request auth" );
}
}];
}