以前一直迷惑,本地推送到底有什么用,后来仔细想了想,确实有用(废话,不然苹果爸爸干嘛开发这个?手动鄙视自己!)
本地通知服务 主要处理基于时间行为的通知。比如定时通知用户该起床撒尿了。
就是这么个用处!嗯,是这样吧?是的!
在iOS10苹果废弃了之前的UILocalNotification,而采用了新的UserNotifications Framework来推送通知。现在先说一下iOS10之前的本地推送流程!
iOS 10之前
注册通知
- (
BOOL)application:(
UIApplication *)application didFinishLaunchingWithOptions:(
NSDictionary *)launchOptions {
if ([[UIDevice currentDevice]
.systemVersion floatValue] >
8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:
nil];
[application registerUserNotificationSettings:settings];
}
return YES;
}
发送通知
UILocalNotification *localNote = [[
UILocalNotification alloc] init];
localNote
.fireDate = [
NSDate dateWithTimeIntervalSinceNow:
5];
localNote
.alertBody = @
"这是一个本地推送";
localNote
.alertAction = @
"看我";
localNote
.hasAction =
YES;
localNote
.alertLaunchImage = @
"../Documents/1.jpg";
localNote
.soundName = UILocalNotificationDefaultSoundName;
localNote
.applicationIconBadgeNumber =
1;
localNote
.userInfo = @{@
"hello" : @
"how are you", @
"msg" : @
"success"};
[[
UIApplication sharedApplication] scheduleLocalNotification:localNote];
这里要说一点,就是iOS系统限制了注册本地推送的数量,最大的注册量为64条。
接收推送
应用在前台或后台,未被杀死时。
- (
void)application:(
UIApplication *)application didReceiveLocalNotification:(
UILocalNotification *)notification{
NSLog(@
"333这里被调用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@
"Title" message:@
"程序在前台或后台,未被杀死,点击通知栏调用" delegate:
self cancelButtonTitle:@
"cancel" otherButtonTitles:@
"ok",
nil];
[alert show];
}
程序已被杀死时
- (
BOOL)application:(
UIApplication *)application didFinishLaunchingWithOptions:(
NSDictionary *)launchOptions {
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
NSLog(@
"666这里被调用");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@
"Title" message:@
"程序已被杀死,点击通知栏调用" delegate:
self cancelButtonTitle:@
"cancel" otherButtonTitles:@
"ok",
nil];
[alert show];
}
return YES;
}
iOS 10之后
先导入这个东西#import <UserNotifications/UserNotifications.h>
注册通知
- (
BOOL)application:(
UIApplication *)application didFinishLaunchingWithOptions:(
NSDictionary *)launchOptions {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center
.delegate =
self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(
BOOL granted,
NSError * _Nullable error) {
}];
return YES;
}
发送通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content
.title = [
NSString localizedUserNotificationStringForKey:@
"本地推送Title" arguments:
nil];
content
.body = [
NSString localizedUserNotificationStringForKey:@
"本地推送Body" arguments:
nil];
content
.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:
5 repeats:
NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@
"FiveSecond"
content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(
NSError * _Nullable error) {
}];
通知处理
实现UNUserNotificationCenterDelegate代理方法:
第一个方法:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
// 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
completionHandler(UNNotificationPresentationOptionSound);
}
这个方法中的那句话就是,当应用在前台的时候,收到本地通知,是用什么方式来展现。系统给了三种形式:
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge =
(1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
第二个代理方法:
这个方法是在后台或者程序被杀死的时候,点击通知栏调用的,在前台的时候不会被调用
- (
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();
}
转载请注明原文地址: https://ju.6miu.com/read-673197.html