一、iOS推送原理
<p>IOS推送消息是許多IOS應用都具備的功能,最近也在研究這個功能,參考了很多資料終于搞定了,下面就把步驟拿出來分享下:</p>
<p>iOS消息推送的工作機制可以簡單的用下圖來概括:</p>
<p>Provider是指某個iPhone軟件運用的Push服務器,APNS是Apple Push Notification Service的縮寫,是蘋果的服務器。</p>
<p>上圖可以分為三個階段:</p>
<p>第一階段:應用程序的服務器把要發送的消息、目的iPhone的標識打包,發給APNS。</p>
<p>第二階段:APNS在自身的已注冊Push服務的iPhone列表中,查找有相應標識的iPhone,并把消息發送到iPhone。 </p>
<p>第三階段:iPhone把發來的消息傳遞給相應的應用程序,并且按照設定彈出Push通知。</p>
<p>從上圖我們可以看到:</p>
<p>1、應用程序注冊消息推送。</p>
<p>2、iOS從APNS Server獲取device token,應用程序接收device token。</p>
<p>3、應用程序將device token發送給PUSH服務端程序。</p>
<p>4、服務端程序向APNS服務發送消息。</p>
<p> 5、APNS服務將消息發送給iPhone應用程序。</p>
<p>無論是iPhone客戶端和APNS,還是Provider和APNS,都需要通過證書進行連接。</p>
二、創建推送證書
1、生成CSR文件
<p>到鑰匙串的證書頒發機構請求證書</p>
<p>填寫你的郵箱和常用名稱,并選擇保存到硬盤</p>
2、創建推送證書
<p>到要創建推送的App ID </p>
<p>點擊右側的 Create Certificate...,</p>
<p>點擊Continue</p>
<p> 選擇CSR文件,選擇剛才創建的CSR文件即可,點擊繼續</p>
<p>創建證書完成,點擊下載可得到要用到的cer證書</p>
<p>創建推送證書完成,App ID詳情頁面</p>
<p>雙擊已經下載的推送證書,到鑰匙串中找到證書,可以導出成為.p12文件</p>
<p>此時,推送證書環境已經完成,接下來代碼開發即可</p>
三、推送程序開發
//
// AppDelegate.m
// 推送
//
// Created by 泛在呂俊衡 on 2017/6/23.
// Copyright ? 2017年 anjohnlv. All rights reserved.
//
#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion floatValue]>=10.0) {
// iOS10以上的
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate=self;
[center requestAuthorizationWithOptions: UNAuthorizationOptionBadge |UNAuthorizationOptionSound |
UNAuthorizationOptionAlert |UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error == nil) {
NSLog(@"注冊成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
}
else {
NSLog(@"注冊失敗");
}
}];
} else if([[UIDevice currentDevice].systemVersion floatValue]>=8.0){
//ios8的
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
} else {
// iOS8以下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
//注冊token
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
//獲取到token,token需要提交到服務器
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"token:%@", deviceToken);
}
//獲取到token失敗
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"tokenError:%@", error.description);
}
//ios6-10接收到信息
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
// iOS 10收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的請求
UNNotificationContent *content = request.content; // 收到推送的消息內容
NSNumber *badge = content.badge; // 推送消息的角標
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標題
NSString *title = content.title; // 推送消息的標題
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 前臺收到遠程通知:%@", userInfo);
}
else {
// 判斷為本地通知
NSLog(@"iOS10 前臺收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置
}
// 通知的點擊事件
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的請求
UNNotificationContent *content = request.content; // 收到推送的消息內容
NSNumber *badge = content.badge; // 推送消息的角標
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標題
NSString *title = content.title; // 推送消息的標題
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 收到遠程通知:%@", userInfo);
}
else {
// 判斷為本地通知
NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
// Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
completionHandler(); // 系統要求執行這個方法
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end