1、準備工做服務器
一、項目中要集成推送,首先要了解推送的原理,雖然3個版本中的推送實現方式不同,可是原理仍是同樣的。安卓老是由於推送出問題,因爲蘋果有本身的推送服務(APNS),因此我以爲蘋果的推送比安卓好實現不少。推送分爲遠程推送和本地推送。遠程推送能夠當作是客戶端,APNS,後臺服務器相互關聯造成的一個服務;本地推送只是在客戶端實現,好比提醒事項,甚至鬧鐘;app
二、遠程推送須要咱們去申請證書,相信大家都已經知道怎麼作了,此處省略幾百字;ide
三、推送的基本流程:fetch
(1)客戶端啓動,註冊推送;ui
(2)註冊成功後,咱們能夠拿到deviceToken,此時咱們把deviceToken發給後臺;spa
(3)咱們實現接受到推送的方法,下面會介紹;代理
(4)須要推送的時候後臺會把推送的信息和deviceToken發給APNS;code
(5)APNS會在已經註冊的deviceToken中查找後臺傳入的deviceToken;orm
(6)找到後,發出通知咱們就能夠接收到了(前提是已經贊成接收通知,而且手機是開機狀態~~)。blog
tps:
推送信息的最大長度是255個字符;
2、註冊推送
註冊推送是在下面的方法中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
一、iOS8之前註冊方式:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
二、iOS8-iOS10註冊方式:
//iOS8的新特性,後臺接收到通知側滑顯示選項
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action_identifier1"; action1.title = @"Accept"; action1.activationMode = UIUserNotificationActivationModeForeground; UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; action2.identifier = @"action_identifier2"; action2.title = @"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground; action2.authenticationRequired = YES; action2.destructive = YES; UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @"category1"; [categorys setActions:@[action1, action2] forContext:(UIUserNotificationActionContextDefault)];
//註冊推送 UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:categorys]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; [[UIApplication sharedApplication] registerUserNotificationSettings:userSettings];
三、iOS10及其以上註冊:
在iOS10中,蘋果對推送進行了一層封裝,須要加入UserNotifications.framework,否則會報錯;而且要導入頭文件:#import <UserNotifications/UserNotifications.h>,由於iOS10纔有這個文件,因此在導入時要加判斷,遵循代理UNUserNotificationCenterDelegate。
#ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
開始註冊:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //遵循代理UNUserNotificationCenterDelegate center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error && granted) { NSLog(@"註冊成功"); }else{ NSLog(@"註冊失敗"); } }]; //獲取通知受權信息 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { UNAuthorizationStatusNotDetermined : 沒有作出選擇 UNAuthorizationStatusDenied : 用戶未受權 UNAuthorizationStatusAuthorized :用戶已受權 }]; [application registerForRemoteNotifications];
3、獲取 deviceToken
在消息註冊成功後,會在下面的方法中返回deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
返回的是data類型,解析的時候看網上不少人說解析失敗,我把解析的方法也放上來供你們參考
NSString *token = [NSString stringWithFormat:@"%@", [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""]];
接着咱們就須要把獲取的token發送給後臺。
4、實現接收到通知的方法
一、iOS10如下,userInfo是後臺傳給APNS的字典,能夠直接使用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { }
二、iOS10及以上
在前臺的時候調用代理方法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
{
}
在方法中,返回UNNotification類型的實例notification,咱們能夠從UNNotification開始逐層點擊查看。獲取後臺傳入APNS最終的方法是:
notification.request.content.userInfo
在後臺的時候調用代理方法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED
{
//這個方法必須調用
completionHandler();
}
在方法中,返回UNNotificationResponse類型的實例response,咱們能夠從UNNotificationResponse開始逐層點擊查看發現UNNotification是UNNotificationResponse的一個屬性,因此獲取後臺傳入APNS最終的方法是:
response.notification.request.content.userInfo