百度推送步驟分析:
1.申請證書
2.創(chuàng)建應(yīng)用
3.配置應(yīng)用
4.配置工程
5.注冊百度云推送并處理回調(diào)
此文主要介紹工程配置和回調(diào)處理
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.設(shè)置開啟Remote notifications奥额,xcode8 中需要注意打開push能力開關(guān)苫幢,如下圖:
注意:打開push能力開關(guān)編譯可能會報錯,這是只需要點擊上圖中的General-->>Signing-->>點掉Automatically manage signing 再點上重新選擇Team
3.在AppDelegate里面
導入頭文件以及設(shè)置判斷是后臺還是前臺觸發(fā)通知的常量靜態(tài)常量
#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 啟動時注冊百度云推送服務(wù)垫挨,需要提供 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];
}
// 此方法是 用戶點擊了通知韩肝,應(yīng)用在前臺 或者開啟后臺并且應(yīng)用在后臺 時調(diào)起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 打印到日志 textView 中
NSLog(@"********** iOS7.0之后 background **********");
//殺死狀態(tài)下,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁面九榔。
if (application.applicationState == UIApplicationStateInactive && !isBackGroundActivateApplication)
{
//? ? ? ? SkipViewController *skipCtr = [[SkipViewController alloc]init];
//? ? ? ? // 根視圖是nav 用push 方式跳轉(zhuǎn)
//? ? ? ? [_tabBarCtr.selectedViewController pushViewController:skipCtr animated:YES];
//? ? ? ? NSLog(@"applacation is unactive ===== %@",userInfo);
/*
// 根視圖是普通的viewctr 用present跳轉(zhuǎn)
[_tabBarCtr.selectedViewController presentViewController:skipCtr animated:YES completion:nil]; */
}
// 應(yīng)用在后臺哀峻。當后臺設(shè)置aps字段里的 content-available 值為 1 并開啟遠程通知激活應(yīng)用的選項
if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"background is Activated Application ");
// 此處可以選擇激活應(yīng)用提前下載郵件圖片等內(nèi)容。
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 系統(tǒng)中哲泊,還需要添加這個方法剩蟀。通過新的 API 注冊推送服務(wù)
- (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 操作否則會失敗
// 網(wǎng)絡(luò)錯誤
if (error) {
return ;
}
if (result) {
// 確認綁定成功
if ([result[@"error_code"]intValue]!=0) {
return;
}
[BPush setTag:@"Mytag" withCompleteHandler:^(id result, NSError *error) {
if (result) {
NSLog(@"設(shè)置tag成功");
}}];}}];
// 當 DeviceToken 獲取失敗時,系統(tǒng)會回調(diào)此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"DeviceToken 獲取失敗切威,原因:%@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// App 收到推送的通知
[BPush handleNotification:userInfo];
NSLog(@"********** ios7.0之前 **********");
// 應(yīng)用在前臺 或者后臺開啟狀態(tài)下育特,不跳轉(zhuǎn)頁面,讓用戶選擇先朦。
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{//殺死狀態(tài)下缰冤,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁面。
}}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"接收本地通知啦T骸C藿!");
[BPush showLocalNotificationAtFront:notification identifierKey:nil];
}