一、iOS推送原理
<p>IOS推送消息是許多IOS應(yīng)用都具備的功能,最近也在研究這個(gè)功能,參考了很多資料終于搞定了绢彤,下面就把步驟拿出來(lái)分享下:</p>
<p>iOS消息推送的工作機(jī)制可以簡(jiǎn)單的用下圖來(lái)概括:</p>
<p>Provider是指某個(gè)iPhone軟件運(yùn)用的Push服務(wù)器,APNS是Apple Push Notification Service的縮寫蜓耻,是蘋果的服務(wù)器茫舶。</p>
<p>上圖可以分為三個(gè)階段:</p>
<p>第一階段:應(yīng)用程序的服務(wù)器把要發(fā)送的消息、目的iPhone的標(biāo)識(shí)打包刹淌,發(fā)給APNS饶氏。</p>
<p>第二階段:APNS在自身的已注冊(cè)Push服務(wù)的iPhone列表中,查找有相應(yīng)標(biāo)識(shí)的iPhone有勾,并把消息發(fā)送到iPhone疹启。 </p>
<p>第三階段:iPhone把發(fā)來(lái)的消息傳遞給相應(yīng)的應(yīng)用程序,并且按照設(shè)定彈出Push通知蔼卡。</p>
<p>從上圖我們可以看到:</p>
<p>1喊崖、應(yīng)用程序注冊(cè)消息推送。</p>
<p>2雇逞、iOS從APNS Server獲取device token荤懂,應(yīng)用程序接收device token。</p>
<p>3塘砸、應(yīng)用程序?qū)evice token發(fā)送給PUSH服務(wù)端程序节仿。</p>
<p>4、服務(wù)端程序向APNS服務(wù)發(fā)送消息掉蔬。</p>
<p> 5廊宪、APNS服務(wù)將消息發(fā)送給iPhone應(yīng)用程序。</p>
<p>無(wú)論是iPhone客戶端和APNS女轿,還是Provider和APNS箭启,都需要通過(guò)證書進(jìn)行連接。</p>
二蛉迹、創(chuàng)建推送證書
1岩调、生成CSR文件
<p>到鑰匙串的證書頒發(fā)機(jī)構(gòu)請(qǐng)求證書</p>
<p>填寫你的郵箱和常用名稱茬末,并選擇保存到硬盤</p>
2睡陪、創(chuàng)建推送證書
<p>到要?jiǎng)?chuàng)建推送的App ID </p>
<p>點(diǎn)擊右側(cè)的 Create Certificate...,</p>
<p>點(diǎn)擊Continue</p>
<p> 選擇CSR文件巴席,選擇剛才創(chuàng)建的CSR文件即可大猛,點(diǎn)擊繼續(xù)</p>
<p>創(chuàng)建證書完成扭倾,點(diǎn)擊下載可得到要用到的cer證書</p>
<p>創(chuàng)建推送證書完成,App ID詳情頁(yè)面</p>
<p>雙擊已經(jīng)下載的推送證書膛壹,到鑰匙串中找到證書,可以導(dǎo)出成為.p12文件</p>
<p>此時(shí),推送證書環(huán)境已經(jīng)完成模聋,接下來(lái)代碼開發(fā)即可</p>
三肩民、推送程序開發(fā)
//
// 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(@"注冊(cè)成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
}
else {
NSLog(@"注冊(cè)失敗");
}
}];
} 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];
}
//注冊(cè)token
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
//獲取到token,token需要提交到服務(wù)器
-(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; // 收到推送的請(qǐng)求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 前臺(tái)收到遠(yuǎn)程通知:%@", userInfo);
}
else {
// 判斷為本地通知
NSLog(@"iOS10 前臺(tái)收到本地通知:{\\\\nbody:%@链方,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@持痰,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法祟蚀,選擇是否提醒用戶工窍,有Badge、Sound前酿、Alert三種類型可以設(shè)置
}
// 通知的點(diǎn)擊事件
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的請(qǐng)求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", 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(); // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
- (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