iOS - 百度推送

來源:互聯網
上載者:User

標籤:

在很多項目開發中,大家都做過推送!例如:極光推送、信鴿推送、個推等等一系列的推送。我們使用的都是整合過後的SDK,其原理都是相同的。
這裡我們來做一下百度推送。

1.百度推送1.1 百度帳號的註冊申請和審核

登入百度雲推送:http://push.baidu.com/fc。填寫相關資料、審核。
審核通過後下載SDK,建立應用(上傳相應的推送認證格式看要求到出),預設建立的應用是,開發人員測試的,上線後要改成生產的。

1.2 SDK的匯入與配置

1)所使用的SDK很簡單。(裡面有個測試音頻text.caf)

2)相應的庫是否匯入

3)認證要帶推送功能

1.3 SDK在項目中的使用

1)啟動時寫的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{//***********************關於如何設定badge角標加1的方法***********************/* 服務端推送的badge是幾就會顯示幾,你只需要跟服務端同步訊息數目,然後讓服務端自己,該推送幾,就推送幾,比如你應用開啟的時候,或者進入背景時候跟服務端同步,這個點,需要你們自己去設計,應用沒有訊息的時候,服務端推送了1,當應用開啟時候,告訴服務端,app沒點擊通知,那下次應用推送2,依次類推。 */
    // 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 測試 開發環境 時需要修改BPushMode為BPushModeDevelopment 需要修改Apikey為自己的Apikey    // 在 App 啟動時註冊百度雲推送服務,需要提供 Apikey    [BPush registerChannel:launchOptions apiKey:@"Apikey改成自己的" pushMode:BPushModeDevelopment withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];    // App 是使用者點擊推送訊息啟動    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    if (userInfo) {        NSLog(@"從訊息啟動:%@",userInfo);        [BPush handleNotification:userInfo];    }     //角標清0    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];}

2)點擊推送訊息

// 此方法是 使用者點擊了通知,應用在前台 或者開啟後台並且應用在後台 時調起- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{     completionHandler(UIBackgroundFetchResultNewData);    NSLog(@"********** iOS7.0之後 background **********");    // 應用在前台 或者後台開啟狀態下,不跳轉頁面,讓使用者選擇。    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//殺死狀態下,直接跳轉到跳轉頁面。    {      //這裡實現頁面跳轉    }}

3)註冊推送服務

// 在 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) {        // 需要在綁定成功後進行 settag listtag deletetag unbind 操作否則會失敗        if (result) {            [BPush setTag:@"Mytag" withCompleteHandler:^(id result, NSError *error) {                if (result) {                    NSLog(@"設定tag成功");                }            }];        }    }];}// 當 DeviceToken 擷取失敗時,系統會回調此方法- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{    NSLog(@"DeviceToken 擷取失敗,原因:%@",error);}

4)收到推送訊息時

- (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];}
1.4 開啟和關閉推送按鈕

說說我的思路和做法:本地存一個參數1或者0 ,例如:status用於存放狀態。加一個按鈕,點擊關閉狀態status存放0,同時UIUserNotificationType設定為:UIUserNotificationTypeNone UIRemoteNotificationType設定為:UIRemoteNotificationTypeNone 反之設定回聲音、徽章、彈窗提醒。
其他的跳轉頁面等操作可以預判所存放狀態status.

其他的具體內容各個推送都有自己具體的詳細文檔,說明的要比我寫的詳盡的多,如有什麼不對不懂得地方可以評論,我會及時解答和修改!

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.