iOS10添加本地推送(Local Notification)实例
前言
iOS10中废弃了UILocalNotification(UIKitFramework)这个类,采用了全新的UserNotificationsFramework来推送通知,从此推送通知也有了自己的标签UN(这待遇真是没别人了),以及对推送功能的一系列增强改进(两个extension和界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点。
本文主要查看了iOS10的相关文档,整理出了在iOS10下的本地推送通知,由于都是代码,就不多做讲解,直接看代码及注释,有问题留言讨论哦。
新的推送注册机制
注册通知(Appdelegate.m):
#import<UserNotifications/UserNotifications.h> #import"AppDelegate.h" @interfaceAppDelegate()<UNUserNotificationCenterDelegate> @end @implementationAppDelegate -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{ //使用UNUserNotificationCenter来管理通知 UNUserNotificationCenter*center=[UNUserNotificationCentercurrentNotificationCenter]; //监听回调事件 center.delegate=self; //iOS10使用以下方法注册,才能得到授权 [centerrequestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionSound) completionHandler:^(BOOLgranted,NSError*_Nullableerror){ //Enableordisablefeaturesbasedonauthorization. }]; //获取当前的通知设置,UNNotificationSettings是只读对象,不能直接修改,只能通过以下方法获取 [centergetNotificationSettingsWithCompletionHandler:^(UNNotificationSettings*_Nonnullsettings){ }]; returnYES; } #pragmamark-UNUserNotificationCenterDelegate //在展示通知前进行处理,即有机会在展示通知前再修改通知内容。 -(void)userNotificationCenter:(UNUserNotificationCenter*)centerwillPresentNotification:(UNNotification*)notificationwithCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{ //1.处理通知 //2.处理完成后条用completionHandler,用于指示在前台显示通知的形式 completionHandler(UNNotificationPresentationOptionAlert); } @end
推送本地通知
//使用UNNotification本地通知 +(void)registerNotification:(NSInteger)alerTime{ //使用UNUserNotificationCenter来管理通知 UNUserNotificationCenter*center=[UNUserNotificationCentercurrentNotificationCenter]; //需创建一个包含待通知内容的UNMutableNotificationContent对象,注意不是UNNotificationContent,此对象为不可变对象。 UNMutableNotificationContent*content=[[UNMutableNotificationContentalloc]init]; content.title=[NSStringlocalizedUserNotificationStringForKey:@"Hello!"arguments:nil]; content.body=[NSStringlocalizedUserNotificationStringForKey:@"Hello_message_body" arguments:nil]; content.sound=[UNNotificationSounddefaultSound]; //在alertTime后推送本地推送 UNTimeIntervalNotificationTrigger*trigger=[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:alerTimerepeats:NO]; UNNotificationRequest*request=[UNNotificationRequestrequestWithIdentifier:@"FiveSecond" content:contenttrigger:trigger]; //添加推送成功后的处理! [centeraddNotificationRequest:requestwithCompletionHandler:^(NSError*_Nullableerror){ UIAlertController*alert=[UIAlertControlleralertControllerWithTitle:@"本地通知"message:@"成功添加推送"preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction*cancelAction=[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil]; [alertaddAction:cancelAction]; [[UIApplicationsharedApplication].keyWindow.rootViewControllerpresentViewController:alertanimated:YEScompletion:nil]; }]; }
iOS10以前本地推送通知:
+(void)registerLocalNotificationInOldWay:(NSInteger)alertTime{ //ios8后,需要添加这个注册,才能得到授权 //if([[UIApplicationsharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]){ //UIUserNotificationTypetype=UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound; //UIUserNotificationSettings*settings=[UIUserNotificationSettingssettingsForTypes:type //categories:nil]; //[[UIApplicationsharedApplication]registerUserNotificationSettings:settings]; ////通知重复提示的单位,可以是天、周、月 //} UILocalNotification*notification=[[UILocalNotificationalloc]init]; //设置触发通知的时间 NSDate*fireDate=[NSDatedateWithTimeIntervalSinceNow:alertTime]; NSLog(@"fireDate=%@",fireDate); notification.fireDate=fireDate; //时区 notification.timeZone=[NSTimeZonedefaultTimeZone]; //设置重复的间隔 notification.repeatInterval=kCFCalendarUnitSecond; //通知内容 notification.alertBody=@"该起床了..."; notification.applicationIconBadgeNumber=1; //通知被触发时播放的声音 notification.soundName=UILocalNotificationDefaultSoundName; //通知参数 NSDictionary*userDict=[NSDictionarydictionaryWithObject:@"开始学习iOS开发了"forKey:@"key"]; notification.userInfo=userDict; //ios8后,需要添加这个注册,才能得到授权 if([[UIApplicationsharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationTypetype=UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound; UIUserNotificationSettings*settings=[UIUserNotificationSettingssettingsForTypes:type categories:nil]; [[UIApplicationsharedApplication]registerUserNotificationSettings:settings]; //通知重复提示的单位,可以是天、周、月 notification.repeatInterval=NSCalendarUnitDay; }else{ //通知重复提示的单位,可以是天、周、月 notification.repeatInterval=NSDayCalendarUnit; } //执行通知注册 [[UIApplicationsharedApplication]scheduleLocalNotification:notification]; }
本文已被整理到了《iOS推送教程》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。