以前一直迷惑,本地推送到底有什么用,后來仔細想了想,確實有用(廢話,不然蘋果爸爸干嘛開發(fā)這個?手動鄙視自己!)
本地通知服務(wù) 主要處理基于時間行為的通知。比如定時通知用戶該起床撒尿了。
就是這么個用處!嗯,是這樣吧?是的!
在iOS10蘋果廢棄了之前的UILocalNotification
,而采用了新的UserNotifications Framework
來推送通知。現(xiàn)在先說一下iOS10之前的本地推送流程!
iOS 10之前
注冊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
return YES;
}
發(fā)送通知
// 1.創(chuàng)建一個本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.設(shè)置通知發(fā)出的時間
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.設(shè)置通知內(nèi)容
localNote.alertBody = @"這是一個本地推送";
// 1.3.設(shè)置鎖屏時,字體下方顯示的一個文字
localNote.alertAction = @"看我";
localNote.hasAction = YES;
// 1.4.設(shè)置啟動圖片(通過通知打開的)
localNote.alertLaunchImage = @"../Documents/1.jpg";
// 1.5.設(shè)置通過到來的聲音
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.6.設(shè)置應(yīng)用圖標左上角顯示的數(shù)字
localNote.applicationIconBadgeNumber = 1;
// 1.7.設(shè)置一些額外的信息
localNote.userInfo = @{@"hello" : @"how are you", @"msg" : @"success"};
// 2.執(zhí)行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
這里要說一點,就是iOS系統(tǒng)限制了注冊本地推送的數(shù)量,最大的注冊量為64條。
接收推送
- 應(yīng)用在前臺或后臺,未被殺死時。
//程序處于前臺或后臺時調(diào)用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"333這里被調(diào)用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序在前臺或后臺,未被殺死,點擊通知欄調(diào)用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
}
- 程序已被殺死時
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
//添加處理代碼
NSLog(@"666這里被調(diào)用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序已被殺死,點擊通知欄調(diào)用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
}
return YES;
}
iOS 10之后
先導入這個東西#import <UserNotifications/UserNotifications.h>
注冊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 使用 UNUserNotificationCenter 來管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//監(jiān)聽回調(diào)事件
center.delegate = self;
//iOS 10 使用以下方法注冊,才能得到授權(quán)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
return YES;
}
發(fā)送通知
// 使用 UNUserNotificationCenter 來管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需創(chuàng)建一個包含待通知內(nèi)容的 UNMutableNotificationContent 對象,注意不是 UNNotificationContent ,此對象為不可變對象。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"本地推送Title" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"本地推送Body" arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 在 設(shè)定時間 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
//添加推送成功后的處理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
通知處理
實現(xiàn)UNUserNotificationCenterDelegate
代理方法:
- 第一個方法:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// 處理完成后條用 completionHandler ,用于指示在前臺顯示通知的形式
completionHandler(UNNotificationPresentationOptionSound);
}
這個方法中的那句話就是,當應(yīng)用在前臺的時候,收到本地通知,是用什么方式來展現(xiàn)。系統(tǒng)給了三種形式:
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
- 第二個代理方法:
這個方法是在后臺或者程序被殺死的時候,點擊通知欄調(diào)用的,在前臺的時候不會被調(diào)用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
completionHandler();
}