1、介绍 数组
通知和推送是应用程序中很重要的组成部分。本地通知能够为应用程序注册一些定时任务,例如闹钟、定时提醒等。远程推送则更强大,提供了一种经过服务端主动推送消息到客户端的方式,服务端能够更加灵活地控制通知逻辑,例如广告的推送、定时任务的提醒、即时通讯类应用离线消息的提醒等。本文先着重着介绍本地通知,因为iOS系统的不断更新,本地通知的API也须要根据设备的系统来进行选择和兼容。app
2、UILocalNotification框架
一、简介ide
ULLocalNotification是iOS8中的一个类(In iOS 8.0 and later),用来实现本地通知功能。通知,其实是由iOS系统管理的一个功能,好比注册了通知,则系统会在通知被触发时给应用程序发送消息。可是,ULLocalNotification仅能提供开发者去编辑消息,消息推送到app上展现的样式和交互则是固定的,开发者自定制的难度至关大。函数
二、添加步骤优化
/// 添加本地推送 -(void)addLocalNotification { //一、建立通知对象 UILocalNotification *notification = [[UILocalNotification alloc] init]; //二、设置触发时间 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//5秒后 //三、设置通知属性 notification.alertTitle = @"本地推送"; /// 通知标题 notification.alertBody = @"HELLO,欢迎哥的到来"; /// 通知主体 notification.applicationIconBadgeNumber = 1; /// 应用程序图标的消息数 notification.hasAction = YES; /// 待机界面开启左滑按钮 notification.alertAction = @"打开应用"; /// 待机界面的滑动按钮提示 notification.userInfo = @{@"name":@"xyq"}; /// 传递的用户数据 notification.soundName = UILocalNotificationDefaultSoundName; /// 在收到通知时播放的声音,默认消息声音 //四、执行本地通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
三、处理逻辑ui
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //若是已经获得受权,就直接添加本地通知,不然申请询问受权 if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone) { //开始受权 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]]; } //若是咱们的应用程序处于关闭状态时,而后被通知唤醒后,直接在完成正常启动流程的代理函数中获取通知对象 UILocalNotification *notification = [launchOptions valueForKey:UIApplicationLaunchOptionsLocationKey]; if (notification) { NSDictionary *userInfo = notification.userInfo; NSLog(@"1----notification------- %@",notification); NSLog(@"1----userInfo------- %@",notification.userInfo); } return YES; }
/// 当用户点击容许或者不容许时,会执行以下代理方法,咱们在其中实现处理逻辑 -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { if (notificationSettings.types != UIUserNotificationTypeNone) { [self addLocalNotification]; } } /// 当咱们的应用进入前台时,须要清除应用图标的数字 -(void)applicationWillEnterForeground:(UIApplication *)application { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } /// 当咱们的应用程序在前台或者从后台进入前台时,收到本地通知 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (notification) { NSDictionary *userInfo = notification.userInfo; NSLog(@"2----notification------- %@",notification); NSLog(@"2----userInfo------- %@",notification.userInfo); } }
四、演示示例atom
2019-11-01 15:01:33.991404+0800 本地推送[95797:2946024] 2----notification------- <UIConcreteLocalNotification: 0x600000932080>{fire date = Friday, November 1, 2019 at 3:01:33 PM China Standard Time, time zone = (null), repeat interval = 0, next fire date = (null), user info = { name = xyq; }} 2019-11-01 15:01:33.991612+0800 本地推送[95797:2946024] 2----userInfo------- { name = xyq; }
3、UserNotificationspa
一、简介设计
UserNotification是iOS10后苹果提出的一个整合的通知和推送框架,对以前的通知和推送功能进行了全面的重构和优化,功能更强大,定制更灵活。表现以下:
二、核心类结构图
注意:
三、权限申请 和 附件资源包位置(Bundle目录下)
//进行用户权限申请 [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) { //在block中会传入布尔值granted,表示用户是否赞成 if (granted) { //若是用户申请权限成功,则能够设置通知中心的代理 //[UNUserNotificationCenter currentNotificationCenter].delegate = self; //添加通知 [self addNormalLocationNotification]; } }];
四、建立通知
4-1:普统统知
/// 建立普通的通知 - (void)addNormalLocationNotification { //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置通知请求发送时APP图标上显示的数字 content.badge = @2; //设置通知的内容 content.body = @"iOS10新通知内容,普统统知,欢迎哥来了"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置触发器 //1-计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //2-周期日历触发器 /* NSDateComponents *components = [[NSDateComponents alloc] init]; components.year = 2019; components.month = 11; components.day = 2; UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO]; //3-地域触发器 CLRegion *region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(33.0, 110.0) radius:100 identifier:@"region"]; UNLocationNotificationTrigger *locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO]; */ //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"添加通知成功"); } }]; }
4-2:图片通知
//建立图片附件通知 -(void)addImageAttachLocationNotification { /* attachments:虽然这是一个数组,可是系统的通知模板只能展现其中的一个附件,设置多个附件也不会有额外的效果,可是若是开发者自定义通知模板UI, 次数组就派上用场了。 */ //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置图片附件 UNNotificationAttachment *imageAttach = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"]] options:nil error:nil]; content.attachments = @[imageAttach]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,图片附件通知,欢迎哥来了"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
4-3:音频通知
//建立音频附件通知 -(void)addAudioAttachLocationNotification { /* attachments:虽然这是一个数组,可是系统的通知模板只能展现其中的一个附件,设置多个附件也不会有额外的效果,可是若是开发者自定义通知模板UI, 次数组就派上用场了。 */ //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置图片附件 UNNotificationAttachment *soundAttach = [UNNotificationAttachment attachmentWithIdentifier:@"soundAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]] options:nil error:nil]; content.attachments = @[soundAttach]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,音频附件通知,欢迎哥来了"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
4-4:视频通知
//建立视频附件通知 -(void)addMoiveAttachLocationNotification { /* attachments:虽然这是一个数组,可是系统的通知模板只能展现其中的一个附件,设置多个附件也不会有额外的效果,可是若是开发者自定义通知模板UI, 次数组就派上用场了。 */ //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置图片附件 UNNotificationAttachment *moiveAttach = [UNNotificationAttachment attachmentWithIdentifier:@"moiveAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"moive" ofType:@"mov"]] options:nil error:nil]; content.attachments = @[moiveAttach]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,视频附件通知,欢迎哥来了"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
五、使用模板
除了上面介绍的强大的附件通知外,咱们还能够把UserNotification提供的模板功能和用户行为利用起来。在iOS系统中,聊天类软件经常采用后台推送的方式推送消息,用户能够在不进入应用程序的状况下,直接在桌面回复经过通知推送过来的消息,这种功能就是经过UNNotificationCategory和UNNotificationAction用户行为来实现的。对于文本回复框,UserNotification框架提供了UNTextInputNotificationAction类,也即UNNotificationAction的子类。
5-1:UNTextInputNotificationAction建立文本回复框
//支持在桌面对本地通知消息进行回复 -(void)supportLocationNotificationReply { //建立回复框 //UNNotificationActionOptionAuthenticationRequired: 须要在解开锁屏后使用 UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"action" title:@"回复" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"发送" textInputPlaceholder:@"请输入回复内容"]; //建立通知模板 UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryText" actions:@[inputAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,普统统知,欢迎哥来了,期待你的回复!!!!"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置通知模板 //categoryIdentifier要与上面建立category的标识保持一致 content.categoryIdentifier = @"myNotificationCategoryText"; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]]; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
5-2:UNNotificationAction建立用户交互按钮
//支持在桌面对本地通知进行按钮交互 -(void)supportLocationNotificationUserInterfaceButton { //建立交互按钮(系统模板最多支持添加4个交互按钮) //UNNotificationActionOptionNone: 无设置 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action" title:@"用户交互按钮1" options:UNNotificationActionOptionNone]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"用户交互按钮2" options:UNNotificationActionOptionNone]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"用户交互按钮3" options:UNNotificationActionOptionNone]; UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"用户交互按钮4" options:UNNotificationActionOptionNone]; //建立通知模板 UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryButton" actions:@[action1,action2,action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,普统统知,欢迎哥来了!!!!"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置通知模板 //categoryIdentifier要与上面建立category的标识保持一致 content.categoryIdentifier = @"myNotificationCategoryButton"; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]]; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationDefault" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
六、通知扩展
经过UserNotification框架,开发者已经能够完成从前很难实现的效果。而后这都不是这个框架最强大的地方,它的最强大的功能是经过扩展实现彻底自定义的经过UI界面。也即Notification Content Extension。在项目新建一个Target后,而后选择Notification Content Extension扩展文件并建立,此时这个扩展文件自带了一个故事板storyBoard和一个NotificationViewCenter类,开发者能够在storyBoard中或者NotificationViewCenter中直接定制须要的UI界面便可,具体方法能够去看API。须要注意的是,NotificationViewCenter类自动遵照了UNNotificationContentExtension协议,这个协议专门用来处理自定义的通知UI的内容展现。
注意:
在自定义的的通知界面上,虽然能够放置按钮或者任何UI控件,但其不能进行用户交互,惟一能够进行交互的方式是经过协议中的媒体按钮及其回调方法。
//当用户点击通知中的用户交互按钮时会调用,开发者能够从notification对象中拿到附件等内容进行UI刷新 - (void)didReceiveNotification:(UNNotification *)notification; - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion; //返回媒体按钮位置 @property (nonatomic, readonly, assign) CGRect mediaPlayPauseButtonFrame; //返回媒体按钮颜色 @property (nonatomic, readonly, copy) UIColor *mediaPlayPauseButtonTintColor; //点击播放和暂停播放按钮的回调 - (void)mediaPlay; - (void)mediaPause; //打开和关闭通知的回调 - (void)performNotificationDefaultAction; - (void)dismissNotificationContentExtension //媒体开始播放和暂停的回调 - (void)mediaPlayingStarted; - (void)mediaPlayingPaused .
当定义好通知的UI模板后,若要使用,还须要在Notification Content扩展中的info.plist文件的NSExtension的NSExtentionAttributes字典中进行一些配置。配置键以下:
6-1:建立扩展
6-2:配置plist
6-3:定制界面
// NotificationViewController.m // MyNotificationContentExtension #import "NotificationViewController.h" #import <UserNotifications/UserNotifications.h> #import <UserNotificationsUI/UserNotificationsUI.h> @interface NotificationViewController () <UNNotificationContentExtension> @property (nonatomic, strong) UILabel *customTitleLabel1; @property (nonatomic, strong) UILabel *customTitleLabel2; @property (nonatomic, strong) UIImageView *customImageView1; @property (nonatomic, strong) UIImageView *customImageView2; @end @implementation NotificationViewController - (void)viewDidLoad { [super viewDidLoad]; //屏幕宽 CGFloat screen_width = [UIScreen mainScreen].bounds.size.width; self.view.backgroundColor = [UIColor redColor]; //自定义Label self.customTitleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screen_width, 40)]; self.customTitleLabel1.textColor = [UIColor whiteColor]; self.customTitleLabel1.textAlignment = NSTextAlignmentCenter; self.customTitleLabel1.backgroundColor = [UIColor greenColor]; //自定义UIImageView self.customImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, screen_width/2, 100)]; self.customImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(screen_width/2, 40, screen_width/2, 100)]; self.customImageView1.backgroundColor = [UIColor purpleColor]; self.customImageView2.backgroundColor = [UIColor blueColor]; //自定义Label self.customTitleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.customImageView1.frame)+20, screen_width, 40)]; self.customTitleLabel2.textColor = [UIColor whiteColor]; self.customTitleLabel2.textAlignment = NSTextAlignmentCenter; self.customTitleLabel2.backgroundColor = [UIColor orangeColor]; //添加控件 [self.view addSubview:self.customTitleLabel1]; [self.view addSubview:self.customTitleLabel2]; [self.view addSubview:self.customImageView1]; [self.view addSubview:self.customImageView2]; } /** 收到通知时触发,可是这个是退出进程以后才使用,只适用于远程推送(因此本地推送,这两个方法是不会执行的) 拿到推送通知内容,刷新自定义的UI */ - (void)didReceiveNotification:(UNNotification *)notification { NSLog(@"notification---------%@",notification); } //用户交互时触发 - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion { NSLog(@"response----------%@",response); } @end
6-4:模板使用
//支持彻底自定义UI的通知 -(void)supportCustomUILocationNotification { //建立交互按钮 UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"action" title:@"自定义的Action" options:UNNotificationActionOptionNone]; //建立通知模板 //"myNotificationCategory"要与plist中配置的保持同样 UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategory" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //通知内容类 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; //设置通知请求发送时APP图标上显示的数字 content.badge = @1; //设置通知的内容 content.body = @"iOS10新通知内容,普统统知,欢迎哥来了"; //设置通知提示音 content.sound = [UNNotificationSound defaultSound]; //设置通知的副标题 content.subtitle = @"这是通知副标题"; //设置通知的标题 content.title = @"这是通知标题"; //设置从通知激活App时的lanunchImage图片 content.launchImageName = @"lun"; //设置通知模板 //categoryIdentifier要与上面建立category的标识保持一致 content.categoryIdentifier = @"myNotificationCategory"; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]]; //设置触发器 //计时器触发器:5s后执行 UNTimeIntervalNotificationTrigger *timrTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //设置通知请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UNNotificationCustomUIH" content:content trigger:timrTrigger]; //添加通知请求 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@""); } }]; }
七、重写媒体按钮
#pragma mark - 重写媒体按钮 //重写媒体按钮的frame - (CGRect)mediaPlayPauseButtonFrame { return CGRectMake(70, 20, 100, 100); } //重写媒体按钮的颜色 - (UIColor *)mediaPlayPauseButtonTintColor { return [UIColor yellowColor]; } //重写媒体按钮类型 - (UNNotificationContentExtensionMediaPlayPauseButtonType)mediaPlayPauseButtonType { return UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault; } //接收媒体按钮播放事件 -(void)mediaPlay { NSLog(@"mediaPlay---------------开始播放"); } //接收媒体按钮暂停事件 -(void)mediaPause { NSLog(@"mediaPause---------------暂停播放");
八、通知的代理方法
UserNotification框架对于通知的回调处理,是经过UNNotificationCenterDelegate协议来实现的。代理方法以下:
#pragma mark - UNUserNotificationCenterDelegate /* 仅当应用程序在前台时,才会调用该方法。 若是未实现该方法或未及时调用该处理程序,则不会显示该通知。 应用程序能够选择将通知显示为声音,徽章,警报和/或显示在通知列表中。 该决定应基于通知中的信息是否对用户可见。 */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { NSLog(@"------------当前应用在前台,收到了通知消息----------------"); completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); } /* 当接收到通知后,在用户点击通知激活应用程序时调用这个方法,不管是在前台仍是后台 */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSLog(@"------------当前应用不管是在前台仍是后台,收到了通知消息,用户点击该消息----------------"); completionHandler(); }
2019-11-02 23:24:47.618298+0800 本地推送[1765:86678] 2019-11-02 23:24:52.636497+0800 本地推送[1765:86538] ------------当前应用在前台,收到了通知消息---------------- 2019-11-02 23:25:21.748096+0800 本地推送[1765:86538] ------------当前应用不管是在前台仍是后台,收到了通知消息,用户点击该消息----------------