前言
iOS 10 中廢棄了 UILocalNotification( UIKit Framework) 這個(gè)類,采用了全新的 UserNotifications Framework 來(lái)推送通知列赎,從此推送通知也有了自己的標(biāo)簽 UN(這待遇真是沒別人了)魏保,以及對(duì)推送功能的一系列增強(qiáng)改進(jìn)(兩個(gè) extension 和 界面的體驗(yàn)優(yōu)化),簡(jiǎn)直是蘋果的親兒子,因此推送這部分功能也成為開發(fā)中的重點(diǎn)。
本文主要查看了 iOS 10 的相關(guān)文檔脂信,整理出了在 iOS 10 下的本地推送通知,由于都是代碼透硝,就不多做講解吉嚣,直接看代碼及注釋,有問題留言討論哦蹬铺。
新的推送注冊(cè)機(jī)制
注冊(cè)通知(Appdelegate.m):
#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 使用 UNUserNotificationCenter 來(lái)管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//監(jiān)聽回調(diào)事件
center.delegate = self;
//iOS 10 使用以下方法注冊(cè),才能得到授權(quán)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
//獲取當(dāng)前的通知設(shè)置秉撇,UNNotificationSettings 是只讀對(duì)象甜攀,不能直接修改秋泄,只能通過(guò)以下方法獲取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
return YES;
}
#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前進(jìn)行處理,即有機(jī)會(huì)在展示通知前再修改通知內(nèi)容规阀。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
//1. 處理通知
//2. 處理完成后條用 completionHandler 恒序,用于指示在前臺(tái)顯示通知的形式
completionHandler(UNNotificationPresentationOptionAlert);
}
@end
推送本地通知
//使用 UNNotification 本地通知
+(void)registerNotification:(NSInteger )alerTime{
// 使用 UNUserNotificationCenter 來(lái)管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需創(chuàng)建一個(gè)包含待通知內(nèi)容的 UNMutableNotificationContent 對(duì)象,注意不是 UNNotificationContent ,此對(duì)象為不可變對(duì)象谁撼。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 在 alertTime 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:alerTime repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
//添加推送成功后的處理歧胁!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}];
}
iOS 10 以前本地推送通知:
+ (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {
// ios8后,需要添加這個(gè)注冊(cè)厉碟,才能得到授權(quán)
// if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
// UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
// categories:nil];
// [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// // 通知重復(fù)提示的單位喊巍,可以是天、周箍鼓、月
// }
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 設(shè)置觸發(fā)通知的時(shí)間
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
// 時(shí)區(qū)
notification.timeZone = [NSTimeZone defaultTimeZone];
// 設(shè)置重復(fù)的間隔
notification.repeatInterval = kCFCalendarUnitSecond;
// 通知內(nèi)容
notification.alertBody = @"該起床了...";
notification.applicationIconBadgeNumber = 1;
// 通知被觸發(fā)時(shí)播放的聲音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知參數(shù)
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學(xué)習(xí)iOS開發(fā)了" forKey:@"key"];
notification.userInfo = userDict;
// ios8后崭参,需要添加這個(gè)注冊(cè),才能得到授權(quán)
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重復(fù)提示的單位款咖,可以是天何暮、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重復(fù)提示的單位铐殃,可以是天海洼、周、月
notification.repeatInterval = NSDayCalendarUnit;
}
// 執(zhí)行通知注冊(cè)
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}