參考文章:
一、iOS10 關于推送
二、iOS10通知框架UserNotification理解與應用
注:此文只能適配iOS10及以上的系統(tǒng),如果需要適配iOS9及以下的系統(tǒng)可讀下面的文章。
iOS8的推送方法可以查看:iOS8-推送(本地和遠程)的簡單使用
在iOS10中,把遠程推送和本地推送整合到了一起,代理方法也是相同的了。所以就一起介紹了。
首先需要添加框架 UserNotifications.framework
(#import <UserNotifications/UserNotifications.h>)
還要遵守通知的代理協(xié)議 <UNUserNotificationCenterDelegate>
推送的注冊
// 同樣是在下面的這個方法中注冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//進行用戶權限的申請
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self; // 遵守代理協(xié)議
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
//在block中會傳入布爾值granted,表示用戶是否同意
if (granted) {
//點擊允許
NSLog(@"注冊通知成功");
} else {
//點擊不允許
NSLog(@"注冊通知失敗");
}
}];
// 如果是注冊遠程推送,還是和以前一樣,需要加上下面這行代碼
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
獲取用戶對通知的設置信息 的方法也不再是原來的方法了。
iOS10以前是一個代理方法:
/** 獲取用戶對通知的設置信息 */
//NS_AVAILABLE_IOS(8_0);
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(@"setting=== %@",notificationSettings);
}
iOS10之后變成了一個實例方法,上面的代理方法不會運行:
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
關于注冊遠程通知的回調方法還是和以前一樣:
/** 遠程通知注冊成功委托 */ //NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 對 deviceToken 進行處理
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
// 需要把 deviceToken 上傳給服務器
/**上傳deviceToken的代碼*/
}
/** 遠程通知注冊失敗委托 */ //NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"%@",error);
}
接下來是iOS10中收到推送通知的方法回調,也是在日常使用中和之前完全不一樣的地方。
在iOS10之前的系統(tǒng)中,本地推送通知和遠程推送的收到推送通知的方法回調是分開的,而且如果攜帶的 Category參數的話,回調方法也是和沒有攜帶參數的方法不同。
但是在iOS10中,無論是本地推送通知還是遠程推送通知,無論有沒有攜帶 Category參數,調用的方法都是一樣的:
#pragma mark --- iOS 10.0
// 程序在前臺的時候對通知的處理。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"=== willPresentNotification");
}
// 程序在后臺但是點擊了收到的推送通知打開程序
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSLog(@"=== didReceiveNotificationResponse");
//處理完消息,最后一定要調用這個代碼塊
completionHandler();
}
獲取和刪除通知
這里通知有兩種狀態(tài):
Pending 等待觸發(fā)的通知
Delivered 已經觸發(fā)展示在通知中心的通知
//獲取未觸發(fā)的通知
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
NSLog(@"pending: %@", requests);
}];
//獲取通知中心列表的通知
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSLog(@"Delivered: %@", notifications);
}];
//清除某一個未觸發(fā)的通知
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]];
//清除某一個通知中心的通知
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]];
//對應的刪除所有通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
創(chuàng)建普通的本地通知
//通知內容類
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
//設置通知請求發(fā)送時 app圖標上顯示的數字
content.badge = @2;
//設置通知的內容
content.body = @"這是iOS10的新通知內容:普通的iOS通知";
//默認的通知提示音
content.sound = [UNNotificationSound defaultSound];
//設置通知的副標題
content.subtitle = @"這里是副標題";
//設置通知的標題
content.title = @"這里是通知的標題";
//設置從通知激活app時的launchImage圖片
content.launchImageName = @"lun";
//設置5S之后執(zhí)行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
//添加通知請求
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];

iOS10的新特性
以上就是關于iOS10推送的簡單使用。當然iOS10的推送相比以前的功能更加豐富了,比如:
1.UserNotification支持自定義通知音效和啟動圖。
2.UserNotification支持向通知內容中添加媒體附件,例如音頻,視頻。
3.UserNotification支持開發(fā)者定義多套通知模板。
4.UserNotification支持完全自定義的通知界面。
5.UserNotification支持自定義通知中的用戶交互按鈕。
關于這些功能,因為目前暫時沒有用到,可以查看我上面列出的參考文章:
iOS10通知框架UserNotification理解與應用