最近武契,再次看路遙 《平凡的世界》時,多多少少有自己的影子在里面荡含,覺得沒有什么愛與不愛的咒唆,在一起就是一種幸福吧。就像潤葉和向前在一起释液,雖然向前失去了雙腿全释,他最終得到一個好老婆和一個可愛的兒子。有時候覺得和誰在一起真的不重要误债,重要是她開心浸船,你也覺得幸福。生活應(yīng)該是柴米油鹽寝蹈,而非風(fēng)花雪月啦李命。
一、 iOS 10 本地推送的效果圖
本文將重點講述如何實現(xiàn) iOS 10的本地推送通知的用法箫老,如下是效果圖封字。
上面的 gif 圖片都已經(jīng)本地推送的如下功能:
- 1、通知附件中有視頻
- 2槽惫、 通知中有通知標題周叮、副標題和通知內(nèi)容
- 3、 通知 action
- 4界斜、前臺和后臺都能接收到通知仿耽。
二、 iOS 10 本地推送實現(xiàn)思路
關(guān)于 iOS 10 本地推送實現(xiàn)思路分為如下三個步驟:
0>各薇、手動導(dǎo)入資源文件项贺,如下圖。
1>峭判、應(yīng)用啟動時开缎,注冊通知,并設(shè)置 Action 按鍵的樣式和標題林螃。
2>奕删、設(shè)置通知的內(nèi)容,附件疗认、通知的觸發(fā)條件完残。
3>伏钠、在代理中處理相對應(yīng)的內(nèi)容。一般是處理 用戶點擊那個按鍵谨设,并處理相對應(yīng)的事件熟掂。
三、 iOS 10 本地代碼的實現(xiàn)
1扎拣、在 AppDelegate.m 中
- A赴肚、首先要導(dǎo)入頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
- B、在 didFinishLaunchingWithOptions 方法中注冊通知二蓝,主要思路是實現(xiàn)如下的內(nèi)容:
1>誉券、注冊通知 requestAuthorizationWithOptions,在此方法中侣夷,注冊通知需要有授權(quán)横朋,設(shè)置推送的聲音、Badge 等四種模式百拓。
2>琴锭、設(shè)置推送的 Action,點擊 Action 按鍵有三種狀態(tài)衙传,UNNotificationActionOptionForeground 直接開啟應(yīng)用决帖,UNNotificationActionOptionDestructive 取消,
UNNotificationActionOptionAuthenticationRequired 需要解鎖
3>蓖捶、將本地推送包裝在 UNNotificationCategory 對象中地回,記得設(shè)置 UNNotificationCategory 對象的標識符。
4>俊鱼、將UNNotificationCategory 對象加入在通知中心去刻像。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {
//ios 10 版本以上的通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//注冊通知
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge| UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"錯誤的要求通知,%@",error);
}];
// Action 內(nèi)容 UNNotificationActionOptionForeground 點擊 actionbutton 會跳轉(zhuǎn)到應(yīng)用中
UNNotificationAction *actionOne = [UNNotificationAction actionWithIdentifier:@"action1" title:@"action1" options:UNNotificationActionOptionForeground];
UNNotificationAction *actionTwo = [UNNotificationAction actionWithIdentifier:@"action2" title:@"action2" options:UNNotificationActionOptionForeground];
UNNotificationAction *actionThree = [UNNotificationAction actionWithIdentifier:@"action3" title:@"action3" options:UNNotificationActionOptionForeground];
//分類
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[actionOne,actionTwo,actionThree] intentIdentifiers:@[] options:0];
//添加分類
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
}else if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0){
//iOS 10 ~ iOS 8 之間的通知
UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];
action1.identifier = @"actionOne";
action1.title = @"actionOne";
action1.activationMode = UIUserNotificationActivationModeForeground;
UIMutableUserNotificationCategory * category = [[UIMutableUserNotificationCategory alloc] init] ;
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:[NSSet setWithObject:category]];
[application registerUserNotificationSettings:setting];
}else if([[UIDevice currentDevice].systemVersion doubleValue] < 8.0){
// 低于 iOS 8 以下的系統(tǒng)
}
// Override point for customization after application launch.
return YES;
}
2并闲、在 ViewController.m 文件中
- A细睡、導(dǎo)入頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
- B、設(shè)置通知代碼
- (void)viewDidLoad {
[super viewDidLoad];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
-
C帝火、推送代理方法的實現(xiàn)
1>溜徙、willPresentNotification 實現(xiàn)此代理方法可以接收來自前臺的通知。當實現(xiàn) completionHandler 時犀填,如果加入 UNNotificationPresentationOptionBadge 時蠢壹,前臺不會接收到通知。2>九巡、didReceiveNotificationResponse 此方法通過 Action 標識來識別用戶點擊那個 Action 按鍵图贸。這樣我們可以通過這個來處理相對應(yīng)的事件。
#pragma mark - UNNotification
//此方法能在前臺收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
if ([response.actionIdentifier isEqualToString:@"action1"]) {
self.noticeImage.image = [UIImage imageNamed:@"1"];
}else if([response.actionIdentifier isEqualToString:@"action2"]){
self.noticeImage.image = [UIImage imageNamed:@"2"];
}else if([response.actionIdentifier isEqualToString:@"action3"]){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"通知標題" message:@"hello world" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
completionHandler();
}
-
D、設(shè)置推送內(nèi)容和附件
Snip20170725_36.png
1>求妹、設(shè)置推送內(nèi)容乏盐,UNMutableNotificationContent 對象中是用來寫推送的內(nèi)容的佳窑,推送的聲音制恍,推送的角標。一定神凑、一定净神、一定要注意,UNMutableNotificationContent 對象 categoryIdentifier 屬性要與注冊的標識一樣溉委,否則不能加載 Action 按鍵鹃唯。
2>、設(shè)置推送附件瓣喊,UNNotificationAttachment 對象是用于處理附件的坡慌,本地推送附件可以是 音頻、視頻藻三、圖片洪橘、gif 動圖。一定一定要注意棵帽,使用本地的資源熄求,不能用網(wǎng)上的資源,否則 UNNotificationAttachment 對象為空逗概。其中資源文件的大小如下截圖弟晚。
3>、觸發(fā)推送逾苫,本地有三種推送方式:時間卿城、日期、地區(qū)铅搓。本案例使用的是 時間 UNTimeIntervalNotificationTrigger瑟押。
4>、將推送的內(nèi)容和觸發(fā)條件包裝在 UNNotificationRequest 對象中狸吞。
5>勉耀、將 UNNotificationRequest 對象 加入在 UNUserNotificationCenter 對象中。
#pragma mark - 發(fā)送通知
- (IBAction)sendNotice:(id)sender {
//1.設(shè)置通知內(nèi)容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"通知標題";
content.subtitle = @"副標題";
content.body = @"iOS 10 本地通知文本內(nèi)容蹋偏,你好吖便斥,親!M肌枢纠!";
content.badge = [NSNumber numberWithInteger:1];
content.categoryIdentifier = @"category";
content.sound = [UNNotificationSound defaultSound];
//2.設(shè)備通知的附件
NSURL *url = [[NSBundle mainBundle] URLForResource:@"flv視頻測試用例1.mp4" withExtension:@""];
UNNotificationAttachment *attchment = [UNNotificationAttachment attachmentWithIdentifier:@"2" URL:url options:nil error:nil];
if (attchment == nil) {
NSLog(@"空的item");
return;
}
content.attachments = @[attchment];
//3.觸發(fā)本地通知
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 repeats:NO];
//4.將通知的信息和觸發(fā)條件放在 request中
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"request" content:content trigger:trigger];
//5. 添加通知
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
}
完整代碼,多謝你完整的讀完本文黎棠,感恩晋渺!