iOS開發三步搞定百度推送

來源:互聯網
上載者:User

標籤:

iOS開發三步搞定百度推送 

百度推送很簡單,準備工作:在百度雲推送平台註冊應用,上傳認證。

步驟一:

百度雲推送平台 http://push.baidu.com/sdk/push_client_sdk_for_ios 

在這裡下載iOS端SDK包,如;

把SDK包裡面的檔案夾拖到你的工程中,如,第一步就這麼簡單。

 

步驟二:

在工程中AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}方法中裡初始化百度推送,代碼如下加粗字型;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

//百度推送

[BPush registerChannel:launchOptions apiKey:@"這裡是百度推送平台給的你的應用的apikey" pushMode:BPushModeProduction withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];

//初始化百度推送

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

[BPush handleNotification:userInfo];

if (userInfo) {

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

// [BPush handleNotification:userInfo];

}

#if TARGET_IPHONE_SIMULATOR

Byte dt[32] = {0xc6, 0x1e, 0x5a, 0x13, 0x2d, 0x04, 0x83, 0x82, 0x12, 0x4c, 0x26, 0xcd, 0x0c, 0x16, 0xf6, 0x7c, 0x74, 0x78, 0xb3, 0x5f, 0x6b, 0x37, 0x0a, 0x42, 0x4f, 0xe7, 0x97, 0xdc, 0x9f, 0x3a, 0x54, 0x10};

[self application:application didRegisterForRemoteNotificationsWithDeviceToken:[NSData dataWithBytes:dt length:32]];

#endif

//角標清0

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

self.mainController = [[MainTabBarViewController alloc] init];

self.window.rootViewController = self.mainController;

[self.window makeKeyAndVisible];

return YES;

}

注意:1、代碼中的apikey需要改成自己應用在百度推送註冊後平台給的apikey;(這個代表你的項目在百度推送平台上的唯一標識)

 

2、代碼中pushMode:的兩種狀態分別是開發狀態和生產狀態,開發人員可以根據自己目前狀態變更。

 

 

步驟三:還是在AppDelegate.m檔案的下述方法中將得到的deviceToken傳給SDK。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

[BPush registerDeviceToken:deviceToken];

[BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {

NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

NSString *channle_id = result[@"channel_id"];

[user setObject:channle_id forKey:@"channel_id"];

[user synchronize];

}];

}

然後在下述兩個方法(方法名很相似,上邊的帶有block,這裡兩個方法裡寫的東西是相同的)中實現相關跳轉就可以啦(給出的例子僅供參考,這裡跳轉用的是用本地通知實現的相應跳轉)註:具體跳轉需要跟後台開發人員制定規則,一起調試。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

type = userInfo[@"push_type"];

// 應用在前台 或者後台開啟狀態下,不跳轉頁面,讓使用者選擇。

if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {

NSLog(@"acitve or background");

UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一條訊息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

[alertView show];

}

else

//殺死狀態下,直接跳轉到跳轉頁面。

{

if ([type isEqualToString:@"comment"]) {

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"reward"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"follow"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"system"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}

}

completionHandler(UIBackgroundFetchResultNewData);

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

[BPush handleNotification:userInfo];

[application setApplicationIconBadgeNumber:5];

type = userInfo[@"push_type"];

// 應用在前台 或者後台開啟狀態下,不跳轉頁面,讓使用者選擇。

if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {

//                NSLog(@"acitve or background");

UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"收到一條訊息" message:userInfo[@"description"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

[alertView show];

}

else//殺死狀態下,直接跳轉到跳轉頁面。

{

if ([type isEqualToString:@"comment"]) {

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToComment" object:nil userInfo:nil];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"reward"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToReward" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"follow"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToFollow" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}else if ([type isEqualToString:@"system"]){

NSNotification *noti  =[NSNotification notificationWithName:@"JumpToSystem" object:nil userInfo:userInfo];

[[NSNotificationCenter defaultCenter] postNotification:noti];

}

}

}

iOS開發三步搞定百度推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.