制作推送證書
iOS工程開發(fā)指引中對(duì)推送流程的概括如下
服務(wù)端的Provider通過(guò)APNs將信息推送給Client App經(jīng)過(guò)兩步:
1 Provider -> APNs//需要蘋果機(jī)構(gòu)頒發(fā)的證書
2 APNs -> Client//需要DeviceToken標(biāo)志App
制作證書之前中捆,介紹一下iOS的設(shè)計(jì)理念: 基于閉環(huán)和安全的思考羊苟,蘋果公司要求使用APNs服務(wù)的開發(fā)者阴汇,提供開發(fā)時(shí)的Mac設(shè)備、App的ID和運(yùn)行App的手機(jī),通過(guò)對(duì)三者的聯(lián)合檢查,基本上能保證確認(rèn)App的唯一性,保證對(duì)AppStore的管理的安全性和可靠性饶唤。
首先蓝翰,我們?cè)?a target="_blank">蘋果開發(fā)者中心光绕,注冊(cè)自己的App的唯一ID:
繼續(xù)直至Done.
然后制作和AppID相綁定的CER證書
點(diǎn)擊continue:
點(diǎn)擊continue,能夠看到需要?jiǎng)?chuàng)建CSR證書畜份,下面有詳細(xì)創(chuàng)建步驟诞帐,這一步可以綁定開發(fā)設(shè)備Mac。英文很簡(jiǎn)單爆雹,和創(chuàng)建發(fā)布證書時(shí)在「鑰匙串訪問(wèn)」中的操作一樣停蕉。
在「鑰匙串訪問(wèn)」中能得到CSR文件
上傳CSR文件
直至Done,下載CER文件:
在「鑰匙串訪問(wèn)」我的證書中顶别,能看到安裝后的結(jié)果:
可以將證書導(dǎo)出谷徙,單獨(dú)存放。以后別人需要驯绎,方便直接發(fā)送完慧。
打開AppID的PushNotification功能
現(xiàn)在,證書已經(jīng)制作好了
在工程中使用證書
確認(rèn)Target的Identify和Signing:
iOS10中剩失,改進(jìn)了推送的代理方法屈尼,增加了3DTouch效果。下面以iOS10的新方法在AppDelegate添加接受通知的代碼:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
if(@available(iOS10.0, *)) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOLgranted,NSError* _Nullable error) {NSLog(@"%@", error); }]; UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:@"GENERAL"actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];// Create the custom actions for expired timer notifications.UNNotificationAction* snoozeAction = [UNNotificationAction actionWithIdentifier:@"SNOOZE_ACTION"title:@"Snooze"options:UNNotificationActionOptionAuthenticationRequired]; UNNotificationAction* stopAction = [UNNotificationAction actionWithIdentifier:@"STOP_ACTION"title:@"Stop"options:UNNotificationActionOptionDestructive]; UNNotificationAction* forAction = [UNNotificationAction actionWithIdentifier:@"FOR_ACTION"title:@"forAction"options:UNNotificationActionOptionForeground];// Create the category with the custom actions.UNNotificationCategory* expiredCategory = [UNNotificationCategory categoryWithIdentifier:@"TIMER_EXPIRED"actions:@[snoozeAction, stopAction,forAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];// Register the notification categories.UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center setDelegate:self]; [center setNotificationCategories:[NSSetsetWithObjects:generalCategory, expiredCategory,nil]]; [[UIApplicationsharedApplication] registerForRemoteNotifications]; }else{ }returnYES;}
#pragma mark - UNUserNotificationCenterDelegate- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions options))completionHandler{NSLog(@"%s", __func__);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{NSLog(@"%s", __func__);}- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)pToken {//保存deviceTokenNSLog(@"regisger success:%@",pToken);}iOS的遠(yuǎn)程推送需要在真機(jī)上調(diào)試拴孤,如果注冊(cè)成功脾歧,就能在didRegisterForRemoteNotificationsWithDeviceToken方法中獲取APNs返回的DeviceToken,在打印欄可以看到演熟。
使用SmartPush調(diào)試
使用SmartPush可以在電腦上方便的模擬APNs推送鞭执。運(yùn)行程序,選擇我們生成的證書和填上打印欄獲得的DeviceToken芒粹,就能在我們的App中看到APNs推送來(lái)的帶有3DTouch功能的通知兄纺。
{
? ? "aps" : {? ? ? ? ? ? ? ? ? // 必須有? ? ?
?? "alert" : {"body":"主體內(nèi)容","title":"標(biāo)題","subtitle":"子標(biāo)題"},
? ? ? ? "body"? : "string",//消息體
? ? ? ? "badge" : 1,//app的icon右上角的推送數(shù)字 在這里設(shè)置
? ? ? ? "sound" : "default"? ??可以為空,為空就是默認(rèn)的聲音
},
? ? "NotiId"? : 20150821,? ? // 自定義key值
}
-----------------------------------------------------------------------------------------------------------------------
支持后臺(tái)推送語(yǔ)音播報(bào)
推送消息
{"aps":{"alert":"This is some fancy message.","content-available":1}}
? ? //后臺(tái)語(yǔ)音播報(bào)
? ? [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
? ? [[AVAudioSession sharedInstance] setActive:YES error:nil];
-(void)applicationWillResignActive:(UIApplication*)application
{
? ? //結(jié)束接受遠(yuǎn)程控制
? ? [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//后臺(tái)語(yǔ)音播報(bào)
- (void)applicationDidEnterBackground:(UIApplication*)application {
? ? UIApplication*? app = [UIApplication sharedApplication];
? ? __block? ? UIBackgroundTaskIdentifier bgTask;
? ? bgTask = [appbeginBackgroundTaskWithExpirationHandler:^{
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? if(bgTask !=UIBackgroundTaskInvalid)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bgTask =UIBackgroundTaskInvalid;
? ? ? ? ? ? }
? ? ? ? });
? ? }];
? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? if(bgTask !=UIBackgroundTaskInvalid)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bgTask =UIBackgroundTaskInvalid;
? ? ? ? ? ? }
? ? ? ? });
? ? });
}
1.推送的大小限制
遠(yuǎn)程通知負(fù)載的大小根據(jù)服務(wù)器使用的API不同而不同化漆。當(dāng)使用HTTP/2 provider API時(shí)估脆,負(fù)載最大為4kB;當(dāng)使用legacy binary interface時(shí)座云,負(fù)載最大為2kB疙赠。當(dāng)負(fù)載大小超過(guò)規(guī)定的負(fù)載大小時(shí),APNs會(huì)拒絕發(fā)送此通知朦拖。
2.推送設(shè)計(jì)原理圖