判斷用戶是否打開了推送
#pragma mark - 是否開啟APP推送
/**是否開啟推送*/
+ (BOOL)isSwitchAppNotification {
if (IOS_VERSION >= 10.0) {
__block BOOL result = NO;
//異步線程中操作是否完成
__block BOOL inThreadOperationComplete = NO;
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
result = NO;
}else if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
result = NO;
}else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
result = YES;
}else {
result = NO;
}
inThreadOperationComplete = YES;
}];
while (!inThreadOperationComplete) {
[NSThread sleepForTimeInterval:0];
}
return result;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
else if (IOS_VERSION >= 8.0)
{
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone != setting.types) {
return YES;
}else {
return NO;
}
}else
{
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone != type) {
return YES;
}else {
return NO;
}
}
#pragma clang diagnostic pop
}
然后如果用戶沒有打開推送按鈕
跳轉到應用相關設置頁面
if (switch.on) {
if (IOS_VERSION >= 10.0) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication]openURL:url options:@{} completionHandler:^(BOOL success) {
}];
}else{
//[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]] 應用標識
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"prefs:root=%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]]];
[[UIApplication sharedApplication]openURL:url];
}
}