當我們的手機在關閉狀態下仍然能收到各種通知,那是我們下載安裝的App主動提示到來的新信息,下面是我手機通知中心的部分截圖:
眾說周知,iOS遠程推送通過APNs實現。APNs是Apple Push Notification service的簡稱,它由Apple獨家提供。遠程推送的服務的實現依據的是服務器和客戶端的長連接,信息通過服務器主動推送(push)給客戶端(我們的手機)。其中Android基于開源的理念,推送策略由第三方自定義,方便的同時也造成了市場上推送技術五花八門的存在。而iOS的推送必須通過Apple的服務器來實現,雖然市面上常用的有極光,環信,融云等第三方的存在,但是它們都是基于Apple的APNs, 優化集成推送的前端工作,最后仍然需要將推送證書和設備標志DeviceToken發送給Apple的服務器來實現遠程推送。
制作推送證書
iOS工程開發指引中對推送流程的概括如下
服務端的Provider通過APNs將信息推送給Client App經過兩步:
1 Provider -> APNs //需要蘋果機構頒發的證書
2 APNs -> Client //需要DeviceToken標志App
制作證書之前,介紹一下iOS的設計理念: 基于閉環和安全的思考,蘋果公司要求使用APNs服務的開發者,提供開發時的Mac設備、App的ID和運行App的手機,通過對三者的聯合檢查,基本上能保證確認App的唯一性,保證對AppStore的管理的安全性和可靠性。
首先,我們在蘋果開發者中心,注冊自己的App的唯一ID:
繼續直至Done.
然后制作和AppID相綁定的CER證書
點擊continue:
點擊continue,能夠看到需要創建CSR證書,下面有詳細創建步驟,這一步可以綁定開發設備Mac。英文很簡單,和創建發布證書時在「鑰匙串訪問」中的操作一樣。
在「鑰匙串訪問」中能得到CSR文件
上傳CSR文件
直至Done,下載CER文件:
雙擊安裝到本機Mac。
在「鑰匙串訪問」我的證書中,能看到安裝后的結果:
可以將證書導出,單獨存放。以后別人需要,方便直接發送。
打開AppID的PushNotification功能
現在,證書已經制作好了,然后我們在對應的工程中愉快的使用了。
在工程中使用證書
確認Target的Identify和Signing:
iOS10中,改進了推送的代理方法,增加了3DTouch效果。下面以iOS10的新方法在AppDelegate添加接受通知的代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, 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:[NSSet setWithObjects:generalCategory, expiredCategory,
nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
}
return YES;
}
#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 {
//保存deviceToken
NSLog(@"regisger success:%@",pToken);
}
iOS的遠程推送需要在真機上調試,如果注冊成功,就能在didRegisterForRemoteNotificationsWithDeviceToken方法中獲取APNs返回的DeviceToken,在打印欄可以看到。
使用SmartPush調試
使用SmartPush可以在電腦上方便的模擬APNs推送。運行程序,選擇我們生成的證書和填上打印欄獲得的DeviceToken,就能在我們的App中看到APNs推送來的帶有3DTouch功能的通知。
喜歡和關注都是對我的鼓勵和支持~
參考文檔:
http://www.lxweimin.com/p/5a4b88874f3a
http://blog.csdn.net/showhilllee/article/details/8631734
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1