(三)iOS百度云推送工程配置以及回調(diào)處理

百度推送步驟分析:

1.申請證書

2.創(chuàng)建應(yīng)用

3.配置應(yīng)用

4.配置工程

5.注冊百度云推送并處理回調(diào)

此文主要介紹工程配置和回調(diào)處理

1.下載SDK以及demo文件,找到BPush.h 、BPushCerResource.bundle、libBPush.a,加入工程,添加時需要注意勾選當(dāng)前Target

SDK需要以下庫:Foundation.framework、CoreTelephony.framework、libz.tbd、SystemConfiguration.framework,CoreLocation.framework 如果使用了idfa版需要添加AdSupport.framework 在工程中添加。

2.設(shè)置開啟Remote notifications,xcode8 中需要注意打開push能力開關(guān),如下圖:


打開通知開關(guān)

注意:打開push能力開關(guān)編譯可能會報錯,這是只需要點擊上圖中的General-->>Signing-->>點掉Automatically manage signing 再點上重新選擇Team

3.在AppDelegate里面

導(dǎo)入頭文件以及設(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:@"回復(fù)" withCategory:@"test" useBehaviorTextInput:YES isDebug:YES];i

// App 是用戶點擊推送消息啟動

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (userInfo) {

NSLog(@"從消息啟動:%@",userInfo);

[BPush handleNotification:userInfo];}

//角標(biāo)清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)用在后臺。當(dāng)后臺設(shè)置aps字段里的 content-available 值為 1 并開啟遠(yuǎn)程通知激活應(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]];

// 需要在綁定成功后進(jìn)行 settag listtag deletetag unbind 操作否則會失敗

// 網(wǎng)絡(luò)錯誤

if (error) {

return ;

}

if (result) {

// 確認(rèn)綁定成功

if ([result[@"error_code"]intValue]!=0) {

return;

}

[BPush setTag:@"Mytag" withCompleteHandler:^(id result, NSError *error) {

if (result) {

NSLog(@"設(shè)置tag成功");

}}];}}];

// 當(dāng) 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(@"接收本地通知啦?。。?);

[BPush showLocalNotificationAtFront:notification identifierKey:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 百度推送很簡單,準(zhǔn)備工作:在百度云推送平臺注冊應(yīng)用,上傳證書。 步驟一: 百度云推送平臺 http://push....
    香餑餑和餓狼的傳說閱讀 3,265評論 2 11
  • 證書的配置方面,我就不多說了,嚴(yán)格按照SDK上的步驟,一步一步的認(rèn)認(rèn)真真的配置證書。 下面我來說一下在APPDel...
    Mrxiaowang閱讀 875評論 0 1
  • 極光推送: 1.JPush當(dāng)前版本是1.8.2,其SDK的開發(fā)除了正常的功能完善和擴(kuò)展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,886評論 10 16
  • 許多集成的步驟個推官網(wǎng)都有了,這里只寫關(guān)于推送的遠(yuǎn)程推送和本地通知的步驟和代碼。APP在后臺時:走蘋果的APNS通...
    AllureJM閱讀 2,967評論 1 9
  • 在簡單項目中,有使用到apns推送服務(wù),許多文章有涉及到卻沒有講清楚。最近做福路通項目,有使用到,做一個總結(jié)。 推...
    天空的守望者閱讀 975評論 0 3

友情鏈接更多精彩內(nèi)容