- (void)registerRemoteNotification:(UIApplication *)application{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
{
//iOS10特有
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// 必须写代理,不然无法监听通知的接收与点击
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 点击允许
DLog(@"注册成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
} else {
// 点击不允许
DLog(@"注册失败");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:@selector(getPushAlert) withObject:nil waitUntilDone:YES];
});
}
}];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
//iOS8 - iOS10
// [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
UIUserNotificationSettings *setting=[[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone!=setting.types) {
DLog(@"注册成功");
}
else
{
DLog(@"注册失败");
这里必须要避开主线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:@selector(getPushAlert) withObject:nil waitUntilDone:YES];
});
}
}else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
// UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
// if(UIRemoteNotificationTypeNone != type) {
// DLog(@"注册成功");
// }else{
// DLog(@"注册失败");
// }
}
// 注册获得device Token
// [[UIApplication sharedApplication] registerForRemoteNotifications];
}
-(void)getPushAlert{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"温馨提示"
message:@"未开启通知,会错过很多重要信息。请到【设置】中开启"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"确定", nil];
alert.tag = 111;
[alert show];
}