本文把 iOS 訊息推送與遠端通知的基礎流程走一遍,重點放在註冊、拿到 DeviceToken 以及把它交給自家伺服器這幾個環節。
/*
開發測試推送機制必須用真機,模擬器收不到。
推送類型:
UIRemoteNotificationTypeNone 不接收推送
UIRemoteNotificationTypeBadge 接收角標數字
UIRemoteNotificationTypeSound 接收提示音
UIRemoteNotificationTypeAlert 接收訊息文字
UIRemoteNotificationTypeNewsstandContentAvailability 接收訂閱內容
想收到註冊後的 deviceToken,還要到蘋果開發者中心完成相關設定。
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 讓系統知道本 App 要接收 APNS 推送
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert
| UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeSound];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - 取得 DeviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"%@", deviceToken);
// 1. 從偏好設定取出舊 token
NSData *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];
// 2. 與新 token 比對
if (![oldToken isEqualToData:deviceToken]) {
// 3. 不一致就存起來
[[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"deviceToken"];
// 4. 用 POST 把新舊 token 送到伺服器
// 1) url:實際位址與參數格式由後端工程師提供
// 2) request:POST body 帶上新舊 token
// 3) connection:走非同步
}
}
再次強調:遠端推送一定要真機才測得出來。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 告訴蘋果伺服器,本 App 需要接收遠端通知
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert
| UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeSound];
return YES;
}
#pragma mark - 取得裝置令牌
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 第一次執行時這個回呼會來得比較慢
NSLog(@"%@", deviceToken);
// 轉成字串方便後續使用
NSString *token = [deviceToken description];
NSLog(@"description %@", token);
// =======================================================
// 若 DeviceToken 變了,要通知伺服器更新
// 每次都把伺服器那邊的 token 記下來,下次取得時比對
NSString *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
// 有變化時用 POST 把新 token 交給伺服器:
// NSURL *url = [NSURL URLWithString:@"公司後台服務器網址"];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
// cachePolicy:NSURLRequestUseProtocolCachePolicy
// timeoutInterval:10.f];
// request.HTTPMethod = @"POST";
// request.HTTPBody = @"轉換後的設備 ID 以及其他資訊[舊 Token]";
// SQL 側的概念就是:
// update t_deviceTable set token = newToken where token = oldToken;
// 同步請求會阻塞流程,非同步則丟給其他執行緒,使用者感覺不到延遲,
// [NSURLConnection sendAsynchronousRequest:request
// queue:[NSOperationQueue mainQueue]
// completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// // 悄悄把裝置資訊送到自家伺服器
// }];
// 先把 token 存進偏好設定
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
}
DeviceToken 可能會變(例如使用者重裝系統、換裝置備份還原),所以每次啟動都該比對一次,別假設它永遠不變。另外 token 對外並非機密,但把它連同使用者識別一起傳輸時,記得走 HTTPS。
以上就是遠端通知最基本的骨架,後續再搭配 APNs 憑證與伺服器就能把訊息送出去。