本地推送通知
本地通知服务主要是处理基于时间行为的通知,比如定时的提醒一些自定义事情,当然还有就是基于区域或位置的通知,主要是当你进入不同的省市会有不同的推送。
在iOS10苹果废弃了之前的UILocalNotification,而采用了新的UserNotifications Framework来推送通知。因此,在注册之前首先要判断系统版本,然后再注册,代码如下:
1.注册
判断系统版本的宏
/**
* 判断系统版本
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
注册是在AppDelegate的didFinishLaunchingWithOptions方法里面进行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//判断系统是否是iOS 10之后
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//设置代理
center.delegate = self;
// 申请通知权限
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"======注册成功要处理的事情========");
}];
}else {
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
}
return YES;
}
2.配置推送内容
iOS 10之前
-(void)pushNotificationWithMsg:(NSDictionary *)params {
//创建本地通知对象
UILocalNotification *localNoti = [[UILocalNotification alloc] init];
//通知触发时间
localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
//设置提醒内容
localNoti.alertBody = @"又一个四季在变幻,我已不是不是那个无悔的少年,青春被时光抛弃已是当父亲的年纪";
//设置弹出框的标题
[localNoti setAlertTitle:@"本地通知测试"];
//通知声音
localNoti.soundName = UILocalNotificationDefaultSoundName;
//设置提醒的按钮文字 / 锁屏时界面底部的闪光文字
localNoti.alertAction = @"锁屏状态下";
//角标
localNoti.applicationIconBadgeNumber = 1;
//设置重复,每隔多久重复发一次推送通知 注意:如果你的通知没有消除,即使卸载了程序,这依然会残留,在下次装入的时候会继续运行
localNoti.repeatInterval = NSCalendarUnitMinute;
//设置通知推送携带的参数,一般用于点击通知打开指定控制器页面
localNoti.userInfo = @{@"selectIndex" : @"xhj"};
}
调度本地通知 ,将本地通知加入本地通知调度池
[[UIApplication sharedApplication] scheduleLocalNotification:localNotifi];
注意:如果重复次数为0的话,通知了一次这个通知就会从系统消除,但大于1的话就需要自己手动删除,否则,即使你卸载了程序,依然会残留,在下次装入的时候还会继续运行
//删除通知
- (void)cancleLocalNoti {
NSArray *notifiArray = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *local in notifiArray) {
//将来可以根据UserInfo的值,来查看这个是否是你想要删除的通知
if (local.userInfo) {
//删除单个通知
[[UIApplication sharedApplication]cancelLocalNotification:local];
}
}
}
iOS 10及以后系统
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需创建一个包含待通知内容的
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];
// 图片
/*
NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"msgImg" withExtension:@"png"];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageIndetifier" URL:imageUrl options:nil error:nil];
content.attachments = @[attachment];
*/
// 创建通知触发时间
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:10 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];
NSLog(@"=======添加推送成功后的处理==========");
}];
3.接收通知
iOS 10之前
- 程序处于前台或后台时候
接收到本地通知后调用AppDelegate的代理方法,当程序处于前台的时候直接返回不做任何处理(通过UIApplicationState的applicationState属性进行判断),如果处于后台根据notification的useinfo进行区分作相应的处理,代码如下:
//接收到本地通知点击处理事件
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self changeLocalNotifi:notification];
}
- (void)changeLocalNotifi:(UILocalNotification *)notification {
// 如果程序处于前台直接返回
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
return;
}
// 获取通知信息
NSString *selectIndex = notification.userInfo[@"selectIndex"];
notification.applicationIconBadgeNumber = 0;
//当通知很多时候,可以通过notification的useinfo来区分不同的通知处理不同的事情
if ([selectIndex isEqualToString:@"xhj"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"我接受到通知并做了相应的处理" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
}
}
判断用户当前是否是激活状态
@property(nonatomic,readonly) UIApplicationState applicationState;
//判断用户当前是否是激活状态 ,枚举值:
UIApplicationStateActive, 激活
UIApplicationStateInactive, 将要激活
UIApplicationStateBackground 后台
- 程序处于退出状态的处理
在 AppDelegate 的 didFinishLaunchingWithOptions 方法中添加如下代码,之后还是调用上面代码中的 changeLocalNotifi: 方法,代码如下:
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotifi = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
[self changeLocalNotifi:localNotifi];
}
- 点击通知处理角标数
当点击通知再次进入程序的时候,app的角标数就要变为0,这是需要在AppDelegate的代理方法applicationWillEnterForeground里面进行如下设置:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
iOS 10及之后
- 程序处于前台时候会走下面这个代理
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"前台收到通知作相应的处理");
}
- 程序处于后台时候(原理同上相同)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
[self changeLocalUNNotifi:response];
}
- (void)changeLocalUNNotifi:(UNNotificationResponse *)response {
// 如果在前台直接返回
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
return;
}
//
NSString *selectIndex = response.notification.request.content.userInfo[@"selectIndex"];
if ([selectIndex isEqualToString:@"xhj"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"都死啦发货啦上过课拉伸的" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
}
}
- 当程序退出的时候处理以及再次进入程序的角标数处理的方法和iOS 10之前的一样。
欢迎大家 看过后,对文章做出评价,与指导!