推送通知(Push Notification)也被稱為遠程通知,是 iOS 3.0 之後加入的功能。它解決的是一個很實際的問題:當 App 沒有啟動、或退到後台時,如何告訴使用者「有新東西了」。
訊息來源是你的伺服器,但真正把訊息送到裝置上的,是蘋果的 APNs(Apple Push Notification Service)。通知可以是新訊息、即將到期的行程,或伺服器上的新資料;呈現方式包括橫幅警示、App 圖示上的數字徽章,以及提示音。使用者可以點開進入 App 處理,也可以選擇忽略,此時 App 不會被啟動。
iPhone、iPad 與 iPod touch 同一時刻只有一個 App 在前台,其餘多在後台待命。推送通知正是讓 App 能在關鍵事件發生時「喊」使用者一聲的管道。
提供者(Provider,也就是你的伺服器)透過串流式 TCP socket,把通知當成二進位資料送給 APNs。APNs 有兩套介面,各有自己的位址與埠:
gateway.sandbox.push.apple.com:2195gateway.push.apple.com:2195不論用哪一介面,都必須透過 TLS/SSL 建立加密通道,並以 SSL 憑證完成身份驗證。
APNs 會保留「送不過去」的紀錄,例如使用者已刪除 App 或長時間未啟動裝置。Provider 應週期性查詢回饋服務,把這些裝置從清單中移除,避免無效重送、白白消耗資源。
feedback.sandbox.push.apple.com:2196feedback.push.apple.com:2196整體流程可以分成三個階段:
而裝置標識是怎麼產生的?順序如下:
兩個名詞釐清:Provider 指某個 App 的推送伺服器,也就是我們要開發的那一端;APNs 則是蘋果的服務端。
開發推送前,必須在 iOS Developer Program 取得 SSL 憑證。每張憑證只綁定一個 App,以 Bundle ID 識別,且分為 sandbox 與 production 兩種用途,兩種環境各有專屬的位址與憑證,描述檔(Provisioning Profile)也需要各自準備。
角色權限上,團隊經理可以建立 Development 與 Distribution SSL 憑證;團隊經理與管理員可建立描述檔;一般成員只能下載安裝。
順帶釐清兩個常被混淆的概念:
aps-environment)。數量可以多個,且通常共用同一張憑證。* 建立。~/Library/MobileDevice/Provisioning Profiles。aps-environment 的值:開發版應為 development,發行版應為 production。openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes
App 必須先向 APNs 註冊,才能收到遠程通知,共三個步驟:
registerForRemoteNotificationTypes:。application:didRegisterForRemoteNotificationsWithDeviceToken: 取得裝置令牌。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// 註冊推送通知:警示、徽章、聲音
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// 判斷 App 是否由使用者點擊通知而啟動
if (launchOptions) {
NSDictionary *pushKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushKey) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
message:@"這是透過推送啟動的程序,你可以在這裡處理推送內容"
delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil];
[alert show];
}
}
return YES;
}
// 註冊成功:取得並上報 deviceToken
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
NSLog(@"apns -> devToken: %@", token);
DeviceSender *sender = [[DeviceSender alloc] initWithDelegate:self];
[sender sendDeviceToPushServer:token];
}
// 註冊失敗
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(@"apns -> 註冊推送時發生錯誤: %@", err);
}
// 收到遠程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"apns -> didReceiveRemoteNotification: %@", userInfo);
// 收到後把圖示上的數字歸零
application.applicationIconBadgeNumber = 0;
NSString *alertText = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
if (alertText != NULL) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"推送訊息"
message:alertText
delegate:self
cancelButtonTitle:@"關閉"
otherButtonTitles:@"處理推送內容", nil];
alert.tag = alert_tag_push;
[alert show];
}
}