iOS本地推送

本地推送:

1, 使用本地推送来引起用户的注意,可以是一个alter弹窗、声音、图片、视频等.例如后台应用程序可以要求系统完成特定任务时显示提醒信息.始终使用本地推送来传达用户需要的重要信息.
2,本地推送弹窗根据你设定的时间和位置进行弹窗推送,第一,如果你的App在后台或者未运行时发送推送,系统会发送弹窗在手机上方,第二,如果你的App在前台运行,系统会发送推送让你在程序中进行需要的处理(代码层).

1
2
3
4
5
#import <UserNotifications/UserNotifications.h>//导入的库,iOS10之后才有
//类
UNUserNotificationCenter
//协议
UNUserNotificationCenterDelegate

推送权限

确定App是否开启了推送允许权限: UNUserNotificationCenter

1
2
3
4
5
6
7
8
9
//1,判断用户是否开启了推送功能
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
//获取到本地推送的权限
NSLog(@"已经获得权限");
}else{
NSLog(@"权限失败");
}
}];

推送内容

配置推送内容: UNMutableNotificationContent

1
2
3
4
5
6
7
8
9
10
11
12
//2,推送内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = @"推送标题";
content.subtitle = @"推送副标题";
content.body = @"推送文字内容";
content.badge = @222;//app角标显示的数量
content.sound = [UNNotificationSound defaultSound];//默认声音
content.userInfo = @{@"A1":@"自己定义的特定内容"};
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"1.png" withExtension:nil];
// UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"i1" URL:url options:nil error:nil];
// content.attachments = @[attachment];//推送附件,可以是图片,视频,音频,这里是一张图片,附件需要存在本地
content.categoryIdentifier = @"currentNoti";//标识符

推送的时刻

配置推送的触发条件:

1,UNTimeIntervalNotificationTrigger //延迟多少秒,当repeats = YES 配置的时间需要大于60s.
2,UNCalendarNotificationTrigger //推送日期, 年,月,日,小时,分钟,秒.
3,UNLocationNotificationTrigger //地点,到达设定好的地点之后推送

1
UNTimeIntervalNotificationTrigger *timerTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:65 repeats:NO];

创建推送的请求

1
2
//4,创建推送的请求, @"currentNoti"是标识符.可以在移除的时候根据标识符找到该推送请求.
UNNotificationRequest *notiRequest = [UNNotificationRequest requestWithIdentifier:@"currentNoti" content:content trigger:timerTrigger];

开启推送

1
2
3
4
5
6
7
8
//5,加入推送中心,开启推送
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notiRequest withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"成功推送了一条本地推送");
}else{
NSLog(@"报错");
}
}];

移除本地推送

1
2
3
4
5
//6,移除local本地推送
//移除所有推送
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
//移除一个推送,或者多个,根据标识符来移除
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"currentNoti"]];

获取当前存在的推送标识

UNNotificationRequest

1
2
3
4
5
6
//获取当前存在的推送UNNotificationRequest
[[UNUserNotificationCenter currentNotificationCenter]getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
for (UNNotificationRequest *req in requests) {
NSLog(@"存在的本地推送标识:%@",req.identifier);
}
}];

推送的协议:UNUserNotificationCenterDelegate

1
2
3
4
5
6
7
8
9
10
11
12
//实现协议
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
//协议中的方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
//是在app内展示推送的设置情况,需要在请求权限的options中添加 providesAppNotificationSettings,iOS12才支持
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
//应用在前台时,接收到消息触发的方法,无论是否点击都会触发
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
//点击了推送的消息,进入app时会触发此方法
}