2017年1月12日
一.百度云推送
目前免費(fèi)(cocoapod不支持)
1.效果圖:
如果程序在后臺(tái)(主頁面tab在第一個(gè))于样,收到通知后的效果 【操作流程:點(diǎn)擊鮮花通知-點(diǎn)擊返回】
如果程序默認(rèn)是開啟狀態(tài)迁霎,效果圖如下:【操作流程:點(diǎn)擊立即前往-點(diǎn)擊返回】
2.如何加載第三方庫,參考官網(wǎng)文檔
基本原理圖:
其實(shí)蘋果用的是deviceToken百宇,我們在如下接口獲取到deviceToken后 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
注冊到百度云庫里面考廉,百度云會(huì)返回一個(gè)對應(yīng)token的userid(之后就是以這個(gè)做設(shè)備識(shí)別)。也就是百度云對應(yīng)的 getChannelId携御,appd客戶端獲取后昌粤,發(fā)送到appServer既可(該值我們是和賬號綁定的,所以后臺(tái)要設(shè)置綁定關(guān)系)啄刹,這樣appServer就可以通過這個(gè)id來推送消息了(群發(fā)的不需要id)涮坐。
2.創(chuàng)建好應(yīng)用后,關(guān)鍵是如下制作證書上傳
具體參照如下:蘋果push證書制作全過程(含測試過程)
二.實(shí)現(xiàn)(工程和代碼相關(guān)設(shè)置)
1.工程設(shè)置
百度云SDK
2.代碼設(shè)置
頭文件設(shè)置
// AppDelegate.m
#import "BPush.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
static BOOL isBackGroundActivateApplication;
第一步:程序啟動(dòng)接口 綁定api key(百度 無賬號登錄體系)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//初始化百度推送組件
[self startBaiDuPush:launchOptions];
return YES;
}
- (void)startBaiDuPush:(NSDictionary *)launchOptions
{
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 測試 開發(fā)環(huán)境 時(shí)需要修改BPushMode為BPushModeDevelopment 需要修改Apikey為自己的Apikey
BPushMode pMode = BPushModeDevelopment;
#ifdef HuBPushModeProduction_ON
pMode = BPushModeProduction;
#endif
NSString *key = Baidu_PUSH_API_KEY;
[BPush registerChannel:launchOptions apiKey:key pushMode:pMode withFirstAction:@"打開" withSecondAction:@"回復(fù)" withCategory:@"test" useBehaviorTextInput:YES isDebug:YES];
// 禁用地理位置推送 需要再綁定接口前調(diào)用。
[BPush disableLbs];
// App 是用戶點(diǎn)擊推送消息啟動(dòng)
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[BPush handleNotification:userInfo];
}
//角標(biāo)清0 角標(biāo)結(jié)合我們自己的邏輯不能清空
// [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
// 在 iOS8 系統(tǒng)中,還需要添加這個(gè)方法驶兜。通過新的 API 注冊推送服務(wù)
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
第二步:將蘋果返回的deviceToken装哆,注冊如百度庫,接著綁定吝秕,之后都用channel_id 替代deviceToken
//獲取手機(jī)唯一標(biāo)示 消息中心push 主推想
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[BPush registerDeviceToken:deviceToken];
//綁定
[BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
if (error) return ;
if (result) {
// 確認(rèn)綁定成功
if ([result[@"error_code"]intValue]!=0) {
return;
}
// 獲取channel_id
NSString *BaiDu_Channel_id = [BPush getChannelId];
[[NSUserDefaults standardUserDefaults]setObject:BaiDu_Channel_id forKey:@"BaiDu_Channel_id"];
}
}];
}
// 當(dāng) DeviceToken 獲取失敗時(shí),系統(tǒng)會(huì)回調(diào)此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"DeviceToken 獲取失敗,原因:%@",error);
}
第三步:處理通知接口
// ios7后的新接口救巷, 此方法是 用戶點(diǎn)擊了通知,應(yīng)用在前臺(tái) 或者開啟后臺(tái)并且應(yīng)用在后臺(tái) 時(shí)調(diào)起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// App 收到推送的通知
[BPush handleNotification:userInfo];
_ps_Type=[userInfo[@"psType"] intValue];//自定義type消息(和后臺(tái)約定)
NSString *message = userInfo[@"aps"][@"alert"];
completionHandler(UIBackgroundFetchResultNewData);
// 應(yīng)用在前臺(tái)句柠,不跳轉(zhuǎn)頁面浦译,讓用戶選擇。
if (application.applicationState == UIApplicationStateActive) {
[self showAlertView:message];
}
// 殺死狀態(tài)下溯职,直接跳轉(zhuǎn)到需要添置跳轉(zhuǎn)頁面精盅。
if (application.applicationState == UIApplicationStateInactive && !isBackGroundActivateApplication)
{
[self dealPushMessage:_ps_Type];
}
// 應(yīng)用在后臺(tái)。當(dāng)后臺(tái)設(shè)置aps字段里的 content-available 值為 1 并開啟遠(yuǎn)程通知激活應(yīng)用的選項(xiàng)
if (application.applicationState == UIApplicationStateBackground) {
// 此處可以選擇激活應(yīng)用提前下載郵件圖片等內(nèi)容谜酒。
isBackGroundActivateApplication = YES;
[self showAlertView:message];
}
}
3.1添加個(gè)人處理相關(guān)個(gè)性化頁面
- (void)showAlertView:(NSString *)message
{
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"消息提醒" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即前往", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self dealPushMessage:_ps_Type];
}
}
//程序主界面首頁底部menutar類型
typedef NS_ENUM(NSInteger, HuMainPageMenuBarType) {
HuMainPageMenuBarTypeWardPatient = 1,//病區(qū)患者
HuMainPageMenuBarTypeEducationClass = 2,//宣教課程
HuMainPageMenuBarTypePersonalCenter = 3,//個(gè)人中心
};
- (void)dealPushMessage:(HuMessagePushType)pushType
{
if (![HuConfigration loginStatus]) {
loginViewController * loginVC = [[loginViewController alloc] init];
[[[UIApplication sharedApplication] delegate] window].rootViewController = loginVC;
}
else
{
switch (pushType) {
case HuMessagePushTypeMain:
{
[self goToMainPage:HuMainPageMenuBarTypeWardPatient];
}
break;
case HuMessagePushTypeDonateFlowerMessage:
{
[self gotoViewControllerStr:@"FlowerMessViewController" WithMenuBar:HuMainPageMenuBarTypePersonalCenter];
}
break;
case HuMessagePushTypeSystemMessage:
{
[kNotificationCenter postNotificationName:kNotificationRed object:nil];
[self gotoViewControllerStr:@"SystemMessagesViewController" WithMenuBar:HuMainPageMenuBarTypePersonalCenter];
}
break;
default:
break;
}
}
}
- (void)gotoViewControllerStr:(NSString*)vcStr WithMenuBar:(HuMainPageMenuBarType)type
{
[self goToMainPage:type];
NSInteger i = type - 1;
//選擇對應(yīng)的控制器數(shù)組叹俏,在將其push進(jìn)入
UINavigationController *nav = [[_customVc childViewControllers] objectAtIndex:i];
UIViewController *vc = [[NSClassFromString(vcStr) alloc] init];
[nav pushViewController:vc animated:YES];
[_customVc hiddenTabBar];
}
- (void)goToMainPage:(HuMainPageMenuBarType)type
{
if (!_customVc)
{
CustomMyViewController *custom = [[CustomMyViewController alloc]init];
custom.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
_window.rootViewController = custom;
_customVc = custom;
}
if(type >= HuMainPageMenuBarTypeWardPatient && type <= HuMainPageMenuBarTypePersonalCenter){
//將主頁底部點(diǎn)擊下班設(shè)置正確
[_customVc tabMenuBarWithType:type];
}
}
第四步.如果獲取的channel_id,需要跟對應(yīng)的賬號綁定甚带,我們就需要程序賬號登錄后她肯,告訴后臺(tái)appServer綁定關(guān)系。之后推送相關(guān)都從后臺(tái)appServer發(fā)起鹰贵。
程序程序之后主界面 綁定下就可以
- (void)viewDidLoad
{
[self bindChannelIdWithAccount];
}
- (void)bindChannelIdWithAccount
{
NSString *BaiDu_Channel_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"BaiDu_Channel_id"];
NSDictionary *param = @{@"deviceChannelId":BaiDu_Channel_id};
NSString *url=[NSString stringWithFormat:@"%@/api/testModle/bindDevice",quanQaunURL];
[QQRequest post:url param:param view:nil success:^(NSDictionary *dic) {
} error:nil failure:nil];
}
三.其他
1.請求Push 常見錯(cuò)誤碼
BPushErrorCode_Success = 0,
BPushErrorCode_MethodTooOften = 22, // 調(diào)用過于頻繁
BPushErrorCode_NetworkInvalible = 10002, // 網(wǎng)絡(luò)連接問題
BPushErrorCode_InternalError = 30600, // 服務(wù)器內(nèi)部錯(cuò)誤
BPushErrorCode_MethodNodAllowed = 30601, // 請求方法不允許
BPushErrorCode_ParamsNotValid = 30602, // 請求參數(shù)錯(cuò)誤
BPushErrorCode_AuthenFailed = 30603, // 權(quán)限驗(yàn)證失敗
BPushErrorCode_DataNotFound = 30605, // 請求數(shù)據(jù)不存在
BPushErrorCode_RequestExpired = 30606, // 請求時(shí)間戳驗(yàn)證超時(shí)
BPushErrorCode_BindNotExists = 30608, // 綁定關(guān)系不存在
2.目前暫時(shí)沒用到(以后可能會(huì)設(shè)計(jì)晴氨,給特點(diǎn)標(biāo)簽的設(shè)備推送消息)
標(biāo)簽組播:推送給打上某一個(gè)標(biāo)簽的一組設(shè)備。為iOS設(shè)備打標(biāo)簽需要通過調(diào)用iOS客戶端SDK中的setTags方法來設(shè)置所屬的Tag 每個(gè)應(yīng)用最多可以定義10000個(gè)標(biāo)簽碉输;每個(gè)標(biāo)簽對應(yīng)的設(shè)備數(shù)沒有限制
3.后臺(tái)推送參數(shù)添加content-available: 1 (靜默推送)【暫時(shí)也沒用】
4.ios9新特性 【 暫時(shí)也沒用】
5.ios10富文本消息 【暫時(shí)也沒用】
2016年11月16日
一.百度云推送測試流程總結(jié)
第一步:登錄如下賬號(百度云網(wǎng)站部署狀態(tài)改變籽前,不會(huì)影響生產(chǎn)的<生產(chǎn)上客服推送是另一套機(jī)制推送>,代碼推送狀態(tài)修改就會(huì)有影響了)
http://push.baidu.com/
二.代碼斷點(diǎn)調(diào)試 獲取百度返回的 BaiDu_Channel_id
三.一般指定設(shè)備id 推送消息
正常現(xiàn)象:
ps:
注意測試的時(shí)候appid一定要選對
如果您發(fā)現(xiàn)本文對你有所幫助枝哄,如果您認(rèn)為其他人也可能受益肄梨,請把它分享出去。