百度推送步骤分析:
1.申请证书
2.创建应用
3.配置应用
4.配置工程
5.注册百度云推送并处理回调
此文主要介绍工程配置和回调处理
1.下载SDK以及demo文件,找到BPush.h 、BPushCerResource.bundle、libBPush.a,加入工程,添加时需要注意勾选当前Target
SDK需要以下库:Foundation.framework、CoreTelephony.framework、libz.tbd、SystemConfiguration.framework,CoreLocation.framework 如果使用了idfa版需要添加AdSupport.framework 在工程中添加。
2.设置开启Remote notifications,xcode8 中需要注意打开push能力开关,如下图:

注意:打开push能力开关编译可能会报错,这是只需要点击上图中的General-->>Signing-->>点掉Automatically manage signing 再点上重新选择Team
3.在AppDelegate里面
导入头文件以及设置判断是后台还是前台触发通知的常量静态常量
#import "BPush.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
static BOOL isBackGroundActivateApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// iOS10 下需要使用新的 API
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if (granted) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
#endif
}
else 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 时需要修改BPushMode为BPushModeProduction 需要修改Apikey为自己的Apikey
// 在 App 启动时注册百度云推送服务,需要提供 Apikey
[BPush registerChannel:launchOptions apiKey:@"需要提供 自己的Apikey" pushMode:BPushModeProduction withFirstAction:@"打开" withSecondAction:@"回复" withCategory:@"test" useBehaviorTextInput:YES isDebug:YES];i
// App 是用户点击推送消息启动
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
NSLog(@"从消息启动:%@",userInfo);
[BPush handleNotification:userInfo];}
//角标清0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
// 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 打印到日志 textView 中
NSLog(@"********** iOS7.0之后 background **********");
//杀死状态下,直接跳转到跳转页面。
if (application.applicationState == UIApplicationStateInactive && !isBackGroundActivateApplication)
{
// SkipViewController *skipCtr = [[SkipViewController alloc]init];
// // 根视图是nav 用push 方式跳转
// [_tabBarCtr.selectedViewController pushViewController:skipCtr animated:YES];
// NSLog(@"applacation is unactive ===== %@",userInfo);
/*
// 根视图是普通的viewctr 用present跳转
[_tabBarCtr.selectedViewController presentViewController:skipCtr animated:YES completion:nil]; */
}
// 应用在后台。当后台设置aps字段里的 content-available 值为 1 并开启远程通知激活应用的选项
if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"background is Activated Application ");
// 此处可以选择激活应用提前下载邮件图片等内容。
isBackGroundActivateApplication = YES;
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
// [self.window.rootViewController addLogString:[NSString stringWithFormat:@"Received Remote Notification :\n%@",userInfo]];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"backgroud : %@",userInfo);
}
// 在 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 bindChannelWithCompleteHandler:^(id result, NSError *error) {
// [self.viewController addLogString:[NSString stringWithFormat:@"Method: %@\n%@",BPushRequestMethodBind,result]];
// 需要在绑定成功后进行 settag listtag deletetag unbind 操作否则会失败
// 网络错误
if (error) {
return ;
}
if (result) {
// 确认绑定成功
if ([result[@"error_code"]intValue]!=0) {
return;
}
[BPush setTag:@"Mytag" withCompleteHandler:^(id result, NSError *error) {
if (result) {
NSLog(@"设置tag成功");
}}];}}];
// 当 DeviceToken 获取失败时,系统会回调此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"DeviceToken 获取失败,原因:%@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// App 收到推送的通知
[BPush handleNotification:userInfo];
NSLog(@"********** ios7.0之前 **********");
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
NSLog(@"acitve or background");
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一条消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}else{//杀死状态下,直接跳转到跳转页面。
}}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"接收本地通知啦!!!");
[BPush showLocalNotificationAtFront:notification identifierKey:nil];
}
网友评论