我感覺我是幸運的,因為當我準備調研推送和webview的時候,蘋果爸爸已經對他們進行觸及靈魂的改造了,我自然可以靠著大樹乘涼,直接入手iOS新版本的推送和iOS8出道的WKWebview。扯皮了這么多,是時候展現真正的技術了!
--------------------------前方蘭博紅溫預警------------------------
本文是iOS推送系列的第一篇,主要講一下實現推送功能之前的準備工作,以及前期的推送注冊,后續將會更新更多關于iOS10推送的新鮮內容。
iOS10的推送相比較之前的來說,真的可以用脫胎換骨來形容,新增了UserNotifications Framework,但使用起來其實很簡單。
一、戰前準備
1.你必須要有1個development證書,如果要發布當然還要有一個distribution證書;
2.你必須要打開工程里的推送開關,不打開則一切皆為虛空。。。
Push Notifications
3.你可能還需要打開Background Modes里的Romote notification,雖然作者現在還有搞清這東西有軟用,還忘知道的同志留言分享。
Reomote notification
4.閱讀蘋果爸爸給孩子們寫的信---官方文檔,既能提升文檔閱讀能力,還能加深對框架的理解,順便把英語給學了,穩賺不賠的買賣。其實看文檔,寫文檔是一個開發人員行走江湖的必備技能。
二、iOS10前后的推送注冊
(附贈兩個三方推送注冊)
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
/* 判斷機型 */
// 建議宏和一些常用參數都添加到項目的config文件中
#define isiOS10 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 10.0)
#define isiOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0)
#define isiOS8 ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
#define isiOS7_1 ([[[UIDevice currentDevice]systemVersion]floatValue] > 7.0)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.系統通知
// 推送注冊,分為iOS10之后和之前
if (ISIOS10) {
// iOS10使用以下方法注冊,才能得到授權
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions types10 = UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) { // 獲取通知授權成功
NSLog(@"Request UNUserNofication authorization succeeded.");
if (granted) { // 點擊允許,這里可以添加一些自己的邏輯
NSLog(@"用戶允許通知");
} else { // 點擊不允許,這里可以添加一些自己的邏輯
NSLog(@"用戶不允許通知");
}
// (2)獲取當前通知, UNNotificationSetting只是讀對象,不能修改,只能通過以下方法獲取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
// 此處查看并設置通知相關信息
}];
} else {
NSLog(@"Request UNUserNofication authorization failed.");
}
}];
}
//iOS8系統以下
else if (isiOS8){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
}
// iOS8系統以下
else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
// 遠程推送注冊-必須加的一個方法!
[[UIApplication sharedApplication] registerForRemoteNotifications];
// 2.UMPush
// (1)UM推送初始化
[UMessage startWithAppkey:UMAppKey launchOptions:launchOptions];
// (2)UM通知注冊
[UMessage registerForRemoteNotifications];
// (3)UM日志
[UMessage setLogEnabled:YES];
// 3.用友有信
// (1)有信IM相關設置
[[YYIMChat sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
// (2)注冊app
[[YYIMChat sharedInstance] registerApp:YYAPPIDNew etpKey:@"cidtech"];
// (3)注冊多方通話
[[YYIMChat sharedInstance].chatManager registerDuduWithAccountIdentify:@"" appkeyTemp:@""];
// (4)添加代理
[[YYIMChat sharedInstance].chatManager addDelegate:self];
// (5)注冊token代理
[[YYIMChat sharedInstance].chatManager registerTokenDelegate:self];
// (6)設置日志級別
[[YYIMChat sharedInstance] setLogLevel:YYIM_LOG_LEVEL_VERBOSE];
// (7)本地推送
[[YYIMChat sharedInstance].chatManager setEnableLocalNotification:YES];
// (8)注冊推送證書
#if defined(DEBUG) && DEBUG
[[YYIMChat sharedInstance] registerApnsCerName:@"你的開發push證書"];
#else
[[YYIMChat sharedInstance] registerApnsCerName:@"你的生產push證書"];
#endif
// (9)設置高德地圖key,參見高德地圖官網(有信IM內置的發送位置功能需要集成高德地圖API)
[MAMapServices sharedServices].apiKey = kYYMapKey;
}
/** 獲取deviceToken 存儲本地以及相關注冊 */
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// 1.有信IM推送注冊
[[YYIMChat sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
// 2.有盟U-Push推送注冊
[UMessage registerDeviceToken:deviceToken];
// 3.deviceToken存儲
NSString *deviceTokenAppend = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[[NSUserDefaults standardUserDefaults] setObject:deviceTokenAppend forKey:DEVICETOKEN];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"deviceToken-------%@",deviceTokenAppend);
}
// iOS10以下系統
else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
}
// 遠程推送注冊-必須加的一個方法!
[[UIApplication sharedApplication] registerForRemoteNotifications];
/** iOS10以下-接收到遠程通知 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self.rootVC.tabBarView selectWithIndex:0];
[self.rootVC selectViewControllerWithIndex:0];
[[YYIMChat sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
[[NSNotificationCenter defaultCenter] postNotificationName:VERIFYNOTIFICATION_REMOTE object:userInfo];
}
/** iOS10以下-接收到本地通知 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[YYIMChat sharedInstance] application:application didReceiveLocalNotification:notification];
}
// iOS10新增:處理前臺收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//應用處于前臺時的遠程推送接受
//關閉友盟自帶的彈出框
[UMessage setAutoAlert:NO];
//必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
} else {
//應用處于前臺時的本地推送接受
}
//當應用處于前臺時提示設置,需要哪個可以設置哪一個
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}
// iOS10新增:處理后臺點擊通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// 必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
// 應用處于后臺時的遠程推送接受
} else {
// 應用處于后臺時的本地推送接受
}
}
以上是iOS10以及之前版本推送和三方推送有盟推送、用友推送的注冊,下面我們就來看看iOS10推送真正令人驚艷的地方
三、真正令人激動的功能
iOS10之前的推送是這樣的
iOS10前的推送
iOS10之后的推送是這樣的
iOS10后的推送
還可以是這樣的
附帶語音的推送