引言:iOS的通知分本地通知和遠程通知,iOS10之前采用的是UILocationNotification類,遠程通知有蘋果服務器進行轉發,本地通知和遠程通知其回調的處理都是通過AppDelegate中的幾個回調方法來完成。iOS10系統中,通知功能的增強是一大優化之處,iOS10中將通知功能整合成了一個框架UserNotification,其結構十分類似於iOS8中的UIWebView向WebKit框架整合的思路。並且UserNotification相比之前的通知功能更加強大,主要表現在如下幾點:
1.通知處理代碼可以從AppDelegate中剝離。
2.通知的注冊,設置,處理更加結構化,更易於模塊化開發。
3.UserNotification支持自定義通知音效和啟動圖。
4.UserNotification支持向通知內容中添加媒體附件,例如音頻,視頻。
5.UserNotification支持開發者定義多套通知模板。
6.UserNotification支持完全自定義的通知界面。
7.UserNotification支持自定義通知中的用戶交互按鈕。
8.通知的觸發更加容易管理。
從上面列舉的幾點就可以看出,iOS10中的UsreNotification真的是一個大的改進,溫故而知新,關於iOS之前版本本地通知和遠程通知的相關內容請查看如下博客:
本地推送:http://my.oschina.net/u/2340880/blog/405491。
遠程推送:http://my.oschina.net/u/2340880/blog/413584。
更多詳細內容可以參考這篇博客:https://my.oschina.net/u/2340880/blog/747781
demo參考:http://www.open-open.com/lib/view/open1472632538972.html
下面就是我自己寫的小程序,小試牛刀一下:
第一步必不可少的肯定是要導入我們的頭文件:<UserNotifications/UserNotifications.h>
然后在AppDelegate.m中注冊通知(第一次寫的時候就是沒有注冊通知,直接就寫了,所以導致通知總是不顯示)
#import "AppDelegate.h"
#import "ViewController.h"
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
//注冊本地推送
// 使用 UNUserNotificationCenter 來管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//監聽回調事件
center.delegate = self;
//iOS 10 使用以下方法注冊,才能得到授權
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
//獲取當前的通知設置,UNNotificationSettings 是只讀對象,不能直接修改,只能通過以下方法獲取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
//
// [self addLocationNotification:5];
return YES;
}
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
//1. 處理通知
//2. 處理完成后條用 completionHandler ,用於指示在前台顯示通知的形式
completionHandler(UNNotificationPresentationOptionAlert);
}
2.然后在ViewController.m文件里發送通知
#import "ViewController.h"
#import <UserNotifications/UserNotifications.h>
@interface ViewController ()
@property (nonatomic, strong) NSString *notitle;//通知標題
@property (nonatomic, strong) NSString *content;//通知內容
@property (nonatomic, strong) NSURL *lineUrl;//跳轉鏈接
@property (nonatomic, strong) NSURL *imageUrl;//附加的圖片
@property (nonatomic, strong) NSData *soundData;//聲音
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"新通知測試";
self.view.backgroundColor = [UIColor whiteColor];
[self setUpUI];
}
- (void)setUpNotification {
//初始化通知
UNMutableNotificationContent *noContent = [[UNMutableNotificationContent alloc] init];
noContent.title = _notitle;//標題
noContent.subtitle = @"副標題";//副標題
noContent.body = _content;//正文
noContent.badge = @1;//
UNNotificationSound *sound = [UNNotificationSound defaultSound];
noContent.sound = sound;
noContent.categoryIdentifier = @"uid";
//5秒后推送通知
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"push" content:noContent trigger:trigger1];
//通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"%@ error",error);
}];
}
- (void)setUpUI {
_notitle = @"通知標題:iOS10測試";
_content = @"這是一條緊急通知";
_lineUrl = [NSURL URLWithString:@"http://www.cnblogs.com/zrr-notes/"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"發送通知" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
btn.frame = CGRectMake(100, 100, 100, 50);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(setUpNotification) forControlEvents:UIControlEventTouchUpInside];
}