基本概念
從技術(shù)而言,信息推送是一項以數(shù)據(jù)挖掘、自然語言處理以及互聯(lián)網(wǎng)等多門技術(shù)為基礎(chǔ)的綜合性方向。將合適的信息推送給合適的人奇适,是一項極具挑戰(zhàn)的工作。這個過程需要對信息作充分的分析芦鳍,并對人的興趣嚷往、行為做細(xì)致的刻畫,并對兩者進(jìn)行有效匹配柠衅。
推送原理
圖中皮仁,Provider是指某個iPhone軟件的Push服務(wù)器,這篇 文章我將使用百度云推送服務(wù)作為Provider。
APNS 是Apple Push Notification Service(Apple Push服務(wù)器)的縮寫贷祈,是蘋果的服務(wù)器趋急。
上圖可以分為三個階段。
第一階段:Push服務(wù)器應(yīng)用程序把要發(fā)送的消息势誊、目的iPhone的標(biāo)識打包呜达,發(fā)給APNS。
第二階段:APNS在自身的已注冊Push服務(wù)的iPhone列表中粟耻,查找有相應(yīng)標(biāo)識的iPhone查近,并把消息發(fā)到iPhone。
第三階段:iPhone把發(fā)來的消息傳遞給相應(yīng)的應(yīng)用程序挤忙, 并且按照設(shè)定彈出Push通知霜威。
從上圖我們可以看到推送的幾個過程步驟分為以下五步:
a. 應(yīng)用程序注冊消息推送
b. IOS跟Push Server要device Token,應(yīng)用程序接受device Token.
c. 應(yīng)用程序?qū)evice Token發(fā)送給PUSH服務(wù)端程序.
d. 服務(wù)端程序向APNS服務(wù)發(fā)送消息.
e. APNS服務(wù)將消息發(fā)送給iPhone應(yīng)用程序.
證書配置
在蘋果開發(fā)者網(wǎng)站中設(shè)置應(yīng)用程序的APP ID,申請推送證書(開發(fā)環(huán)境,生產(chǎn)環(huán)境),配置文件.修改項目中Info.plist中的Bundle ID和證書中的Bundle ID一致
下載百度云SDK并導(dǎo)入相關(guān)的依賴庫.
以下以百度云推送為例在Appdelegate中作相應(yīng)的demo配置.
創(chuàng)建應(yīng)用
在常用的第三方推送平臺創(chuàng)建應(yīng)用,并綁定標(biāo)識,添加之前申請的推送證書,獲取到key值;
項目中配置推送消息
在Appdelegate中導(dǎo)入BPush.h, 并在- -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中添加一下代碼,并修改apiKey為剛才創(chuàng)建應(yīng)用所得appKey,推送模式為開發(fā)模式
// 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 測試 開發(fā)環(huán)境 時需要修改BPushMode為BPushModeDevelopment 需要修改Apikey為自己的Apikey
// 在 App 啟動時注冊百度云推送服務(wù),需要提供 Apikey
[BPush registerChannel:launchOptions apiKey:<#在百度云推送官網(wǎng)上注冊后得到的apikey#> pushMode:BPushModeDevelopment withFirstAction:nil withSecondAction:nil withCategory:nil isDebug:YES];
添加以下3個方法
// 在 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:nil];
}
// 當(dāng) DeviceToken 獲取失敗時叁执,系統(tǒng)會回調(diào)此方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"DeviceToken 獲取失敗茄厘,原因:%@",error);
}
##在百度云推送后臺創(chuàng)建一個通知測試
請選擇開發(fā)模式,然后填寫要發(fā)送的消息內(nèi)容,然后發(fā)送消息給應(yīng)用程序.