iOS开发三步搞定百度推送
iOS开发三步搞定百度推送
百度推送很简单,准备工作:在百度云推送平台注册应用,上传证书。
步骤一:
百度云推送平台 http://push.baidu.com/sdk/push_client_sdk_for_ios
在这里下载iOS端SDK包,如下图;
把SDK包里面的下图文件夹拖到你的工程中,如下图,第一步就这么简单。
步骤二:
在工程中AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}方法中里初始化百度推送,代码如下加粗字体;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//百度推送
[BPush registerChannel:launchOptions apiKey:@"这里是百度推送平台给的你的应用的apikey" pushMode:BPushModeProduction withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];
//初始化百度推送
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[BPush handleNotification:userInfo];
if (userInfo) {
NSLog(@"从消息启动:%@",userInfo);
// [BPush handleNotification:userInfo];
}
#if TARGET_IPHONE_SIMULATOR
Byte dt[32] = {0xc6, 0x1e, 0x5a, 0x13, 0x2d, 0x04, 0x83, 0x82, 0x12, 0x4c, 0x26, 0xcd, 0x0c, 0x16, 0xf6, 0x7c, 0x74, 0x78, 0xb3, 0x5f, 0x6b, 0x37, 0x0a, 0x42, 0x4f, 0xe7, 0x97, 0xdc, 0x9f, 0x3a, 0x54, 0x10};
[self application:application didRegisterForRemoteNotificationsWithDeviceToken:[NSData dataWithBytes:dt length:32]];
#endif
//角标清0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
self.mainController = [[MainTabBarViewController alloc] init];
self.window.rootViewController = self.mainController;
[self.window makeKeyAndVisible];
return YES;
}
注意:1、代码中的apikey需要改成自己应用在百度推送注册后平台给的apikey;(这个代表你的项目在百度推送平台上的唯一标识)
2、代码中pushMode:的两种状态分别是开发状态和生产状态,开发者可以根据自己目前状态进行更改。
步骤三:还是在AppDelegate.m文件的下述方法中将得到的deviceToken传给SDK。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[BPush registerDeviceToken:deviceToken];
[BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *channle_id = result[@"channel_id"];
[user setObject:channle_id forKey:@"channel_id"];
[user synchronize];
}];
}
然后在下述两个方法(方法名很相似,上边的带有block,这里两个方法里写的东西是相同的)中实现相关跳转就可以啦(给出的例子仅供参考,这里跳转用的是用本地通知实现的相应跳转)注:具体跳转需要跟后台开发人员制定规则,一起调试。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
type = userInfo[@"push_type"];
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
else
//杀死状态下,直接跳转到跳转页面。
{
if ([type isEqualToString:@"comment"]) {
NSNotification *noti =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"reward"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"follow"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"system"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}
}
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[BPush handleNotification:userInfo];
[application setApplicationIconBadgeNumber:5];
type = userInfo[@"push_type"];
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
// NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
else//杀死状态下,直接跳转到跳转页面。
{
if ([type isEqualToString:@"comment"]) {
NSNotification *noti =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"reward"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"follow"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}else if ([type isEqualToString:@"system"]){
NSNotification *noti =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:noti];
}
}
}