兼容iOS7以上设置推送,跳转app设置界面

本文详细介绍了如何在iOS应用中实现推送通知的管理,包括推送开关的配置、跳转到系统设置界面的方法,以及如何检查用户是否允许推送通知。同时提供了跳转到iOS7应用设置界面的代码示例,确保了应用在不同iOS版本上的兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先我们要明确推送实际上有2个开关:

   (1)iOS系统(APNS协作)   

   (2)本地(我们自己的)服务器

            所以,为了更好的用户体验,并且iOS有限制的情况下:

            从关闭到打开,只能跳转APP 在系统设置 里的页面。   

            从打开到关闭,将是否接收推送的设置传给服务器端,服务器根据设置选择是否发送推送。假如某用户关闭推送,服务就不给改用户发送推送了。

2、另外一个要解决的是跳转iOS7 的app 设置界面的功能,附上代码:

//跳转到当前app的设置推送界面, iOS7的有点问题(未验证能否通过appstore审核)

+ (void)JumpToPushSetting

{

    if ([self getOSVersion] >= 8.0f) {

        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

        

        if([[UIApplication sharedApplication] canOpenURL:url]) {

            

            NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];

            [[UIApplication sharedApplication] openURL:url];

        }

    }

    else {

        NSString *destPath = [NSString stringWithFormat:@"prefs:root=NOTIFICATIONS_ID&path=%@", [self getBundleID]];

        NSURL*url2=[NSURL URLWithString:destPath];

        [[UIApplication sharedApplication] openURL:url2];

    }

}

//是否允许推送

+ (BOOL)IsAllowedNotification {

    //iOS8 check if user allow notification

    if ([self getOSVersion] >= 8.0f) {// system is iOS8

        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (UIUserNotificationTypeNone != setting.types) {

            return YES;

        }

        else

            return NO;

    } else {//iOS7

        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

        if(UIRemoteNotificationTypeNone != type)

            return YES;

        else

            return NO;

    }

    

    return NO;

}


//注册推送消息显示方式

+ (void)RegistPushNotificationSettings

{

    UIApplication *application = [UIApplication sharedApplication];

    //这下面注册,在第一次打开app的时候,会显示给用户是否允许推送通知窗口

    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])

    {

        //IOS8

        //创建UIUserNotificationSettings,并设置消息的显示类类型

        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];

        

        [application registerUserNotificationSettings:notiSettings];

        

    } else{ // ios7

        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                                       |UIRemoteNotificationTypeSound                                      |UIRemoteNotificationTypeAlert)];

    }

}


+(NSString*)getAppVersion

{

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

    //NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];

    NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

    //NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];

    return version;

}


//获取APP本地的bundle ID

+(NSString*)getBundleID

{

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

    NSString* bundleID = [infoDictionary objectForKey:@"CFBundleIdentifier"];

    return bundleID;

}


3、以下是流程图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值