阅读更多
1.倒入百度 SDK 第三方包
2.添加依赖框架:
LibZ.dylib
CoreGraphics.frameWork
CoreTelePhony.framework
SystemConfiguration.framework
3.添加百度SDK所依赖的JSONKit 第三方文件 并对jsonKit 禁arc处理。
做完如上的三步就开始代码的实现了:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[[UINavigationBar appearance] setTintColor:[UIColor grayColor]];
[application setStatusBarHidden:YES];
// iOS8 下需要使用新的 API
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
#warning 上线 AppStore 时需要修改 pushMode
// 在 App 启动时注册百度云推送服务,需要提供 Apikey
[BPush registerChannel:launchOptions apiKey:@"your keys" pushMode:BPushModeDevelopment isDebug:YES];
// [BPush setupChannel:launchOptions];
// 设置 BPush 的回调
[BPush setDelegate:self];
// App 是用户点击推送消息启动
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
NSLog(@"从消息启动:%@",userInfo);
[BPush handleNotification:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 打印到日志 textView 中
// [self.viewController addLogString:[NSString stringWithFormat:@"backgroud : %@",userInfo]];
completionHandler(UIBackgroundFetchResultNewData);
}
// 在 iOS8 系统中,还需要添加这个方法。通过新的 API 注册推送服务
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"test:%@",deviceToken);
[BPush registerDeviceToken:deviceToken];
[BPush bindChannel];
// 打印到日志 textView 中
// [self.viewController addLogString:[NSString stringWithFormat:@"Register use deviceToken : %@",deviceToken]];
}
// 当 DeviceToken 获取失败时,系统会回调此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"DeviceToken 获取失败,原因:%@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// App 收到推送的通知
[BPush handleNotification:userInfo];
// [self.viewController addLogString:[NSString stringWithFormat:@"Received Remote Notification :\n%@",userInfo]];
NSLog(@"%@",userInfo);
}
#pragma mark Push Delegate
- (void)onMethod:(NSString*)method response:(NSDictionary*)data
{
// [self.viewController addLogString:[NSString stringWithFormat:@"Method: %@\n%@",method,data]];
}