iOS 本地推送(通知)

以前一直迷惑,本地推送到底有什么用,后来仔细想了想,确实有用(废话,不然苹果爸爸干嘛开发这个?手动鄙视自己!)

本地通知服务 主要处理基于时间行为的通知。比如定时通知用户该起床撒尿了。

就是这么个用处!嗯,是这样吧?是的!

在iOS10苹果废弃了之前的UILocalNotification,而采用了新的UserNotifications Framework来推送通知。现在先说一下iOS10之前的本地推送流程!

iOS 10之前

注册通知

1
2
3
4
5
6
7
8
9
10
11
12
- (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;
}

发送通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 1.创建一个本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];

// 1.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

// 1.2.设置通知内容
localNote.alertBody = @"这是一个本地推送";

// 1.3.设置锁屏时,字体下方显示的一个文字
localNote.alertAction = @"看我";
localNote.hasAction = YES;

// 1.4.设置启动图片(通过通知打开的)
localNote.alertLaunchImage = @"../Documents/1.jpg";

// 1.5.设置通过到来的声音
localNote.soundName = UILocalNotificationDefaultSoundName;

// 1.6.设置应用图标左上角显示的数字
localNote.applicationIconBadgeNumber = 1;

// 1.7.设置一些额外的信息
localNote.userInfo = @{@"hello" : @"how are you", @"msg" : @"success"};

// 2.执行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];

这里要说一点,就是iOS系统限制了注册本地推送的数量,最大的注册量为64条。

接收推送

  • 应用在前台或后台,未被杀死时。
1
2
3
4
5
6
7
//程序处于前台或后台时调用
- (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];
}
  • 程序已被杀死时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.


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>

注册通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
center.delegate = self;

//iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];


return YES;
}

发送通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

//需创建一个包含待通知内容的 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];

// 在 设定时间 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];

UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];

//添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];

通知处理

实现UNUserNotificationCenterDelegate代理方法:

  • 第一个代理方法:
1
2
3
4
5
6
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{


// 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
completionHandler(UNNotificationPresentationOptionSound);
}

这个方法中的那句话就是,当应用在前台的时候,收到本地通知,是用什么方式来展现。系统给了三种形式:

1
2
3
4
5
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
  • 第二个代理方法:

这个方法是在后台或者程序被杀死的时候,点击通知栏调用的,在前台的时候不会被调用

1
2
3
4
5
6
7
- (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();
}
-------------本文结束感谢您的阅读-------------
分享使我快乐!