iOS里很简单的推送,移动端。

Apple公司里的推送相比安卓真的很简单,因为系统已经完全通过ASPN实现好了。废话不多具体代码:

iOS 实现推送要实现三段代码。

第一段:在AppDelegate里的,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 获取系统版本号

    double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。

    

    if(version>=8.0f){

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        [application registerForRemoteNotifications];

    }else{

        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];

    }

}

将这段代码粘贴过去就OK了。

第二段:给AppDelegate添加两个方法,

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    NSString *token = [NSString stringWithFormat:@"%@", deviceToken];

    

    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];

    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];

    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

    

    //保存token到用户默认设置中

    NSUserDefaults *userDefault =[NSUserDefaults standardUserDefaults] ;

    [userDefault setValue:token forKey:@"gjxq_token"];

}


- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    

}

当你的应用收到苹果返回的Token,会调用第一个方法;如果失败,则会调用第二个方法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值