云推送(Push)是百度开放云向开发者提供的消息推送服务;通过利用云端与客户端之间建立稳定、可靠的长连接来为开发者提供向客户端应用推送实时消息服务。
百度云推送提供了三种推送方式:通知、消息、富媒体,ios只支持通知。android都支持。
官网:
SDK:
http://developer.baidu.com/cloud/push
文档中心:
http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guideios
群 348807820 创建者 467086835
准备证书:
* 开发阶段: 需要一个开发阶段类型的推送证书生成 dev_push.cer 和 dev_push.p12 一个开发阶段类型的 dev.cer 一个配置文件 dev_profile.mobileprovision (对应的app id 不可带通配符) * 发布阶段: 需要一个发布阶段类型的推送证书生成 dis_push.cer 和 dis_push.p12 一个发布阶段类型的 dis.cer 一个配置文件 dis_profile.mobileprovision (对应的app id 不可带通配符)
生成推送证书的pem文件MyApnsCert.pem;
(后台和APNS服务器通信使用,在设置推送设置时我们需要将这个文件提供给baiduPush,如何生成在官方文档里有)
在开发者服务管理中添加应用、设置推送设置。
工程修改
添加BPushConfig.plist 配置文件:
内容如下: API_KEY对应的管理平台中申请时配置的API_KEY
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>PRODUCTION_MODE</key> <false/> <key>DEBUG</key> <true/> <key>API_KEY</key> <string>*******************</string> </dict> </plist>
UIAppliationDelegate中代码:
#pragma mark - 启动 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //注册推送的设置信息 [self registerUserNotificationSettings]; return YES; } #pragma mark - 注册推送的设置信息 -(void)registerUserNotificationSettings{ //* BPush //[BPush setupChannel:launchOptions]; //[BPush setDelegate:self]; //* APNS //设置badge //[application setApplicationIconBadgeNumber:0]; //注册推送的设置信息 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { //UIRemoteNotificationTypeBadge | UIUserNotificationType myTypes = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else{ UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; } } #pragma mark - 推送回调 ///回调 - APP成功注册了苹果推送通知服务(APN)时调用 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"test:%@",deviceToken); [BPush registerDeviceToken: deviceToken]; [BPush bindChannel]; // 必须。可以在其它时机调用,只有在该方法返回(通过onMethod:response:回调)绑定成功时,app才能接收到Push消息。一个app绑定成功至少一次即可(如果access token变更请重新绑定)。 self.viewController.textView.text = [self.viewController.textView.text stringByAppendingFormat: @"Register device token: %@\n openudid: %@", deviceToken, [OpenUDID value]]; } ///回调 - APP注册苹果推送通知服务(APN)失败时调用 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Failed To Regist APNS : %@",error); } ///回调 - 当接收到远程推送的时候调用。 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"Receive Notify: %@", [userInfo JSONString]); NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; if (application.applicationState == UIApplicationStateActive) { // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"The application received this remote notification while it was running:\n%@", alert] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } [application setApplicationIconBadgeNumber:0]; [BPush handleNotification:userInfo]; self.viewController.textView.text = [self.viewController.textView.text stringByAppendingFormat:@"Receive notification:\n%@", [userInfo JSONString]]; } ///回调 - 当采用registerUserNotificationSettings:注册了通知设置 (IOS8及其以后需要) #if SUPPORT_IOS8 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //返回 注册的 notificationSettings //register to receive notifications [application registerForRemoteNotifications]; } #endif #pragma mark BPushDelegate //6. 实现BPushDelegate协议,必须实现方法onMethod:response:: // 必须,如果正确调用了setDelegate,在bindChannel之后,结果在这个回调中返回。 // 若绑定失败,请进行重新绑定,确保至少绑定成功一次 - (void) onMethod:(NSString*)method response:(NSDictionary*)data { NSLog(@"On method:%@", method); NSLog(@"data:%@", [data description]); NSDictionary* res = [[[NSDictionary alloc] initWithDictionary:data] autorelease]; if ([BPushRequestMethod_Bind isEqualToString:method]) { NSString *appid = [res valueForKey:BPushRequestAppIdKey]; NSString *userid = [res valueForKey:BPushRequestUserIdKey]; NSString *channelid = [res valueForKey:BPushRequestChannelIdKey]; //NSString *requestid = [res valueForKey:BPushRequestRequestIdKey]; int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue]; if (returnCode == BPushErrorCode_Success) { self.viewController.appidText.text = appid; self.viewController.useridText.text = userid; self.viewController.channelidText.text = channelid; // 在内存中备份,以便短时间内进入可以看到这些值,而不需要重新bind self.appId = appid; self.channelId = channelid; self.userId = userid; } } else if ([BPushRequestMethod_Unbind isEqualToString:method]) { int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue]; if (returnCode == BPushErrorCode_Success) { self.viewController.appidText.text = nil; self.viewController.useridText.text = nil; self.viewController.channelidText.text = nil; } } self.viewController.textView.text = [[[NSString alloc] initWithFormat: @"%@ return: \n%@", method, [data description]] autorelease]; }
应用继承sdk具体步骤参照文档中心:
http://developer.baidu.com/wiki/index.php?title=docs/cplat/push
有没有一个推送平台?(有)有一个发送消息的管理平台
提供的服务器端调用的接口吗?(有)