iOS10添加本地推送(Local Notification)实例
程序员文章站
2023-12-18 17:53:58
前言
ios 10 中废弃了 uilocalnotification ( uikit framework ) 这个类,采用了全新的 usernotifications...
前言
ios 10 中废弃了 uilocalnotification ( uikit framework ) 这个类,采用了全新的 usernotifications framework 来推送通知,从此推送通知也有了自己的标签 un (这待遇真是没别人了),以及对推送功能的一系列增强改进(两个 extension 和 界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点。
本文主要查看了 ios 10 的相关文档,整理出了在 ios 10 下的本地推送通知,由于都是代码,就不多做讲解,直接看代码及注释,有问题留言讨论哦。
新的推送注册机制
注册通知( appdelegate.m ):
#import <usernotifications/usernotifications.h> #import "appdelegate.h" @interface appdelegate ()<unusernotificationcenterdelegate> @end @implementation appdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // 使用 unusernotificationcenter 来管理通知 unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter]; //监听回调事件 center.delegate = self; //ios 10 使用以下方法注册,才能得到授权 [center requestauthorizationwithoptions:(unauthorizationoptionalert + unauthorizationoptionsound) completionhandler:^(bool granted, nserror * _nullable error) { // enable or disable features based on authorization. }]; //获取当前的通知设置,unnotificationsettings 是只读对象,不能直接修改,只能通过以下方法获取 [center getnotificationsettingswithcompletionhandler:^(unnotificationsettings * _nonnull settings) { }]; return yes; } #pragma mark - unusernotificationcenterdelegate //在展示通知前进行处理,即有机会在展示通知前再修改通知内容。 -(void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler{ //1. 处理通知 //2. 处理完成后条用 completionhandler ,用于指示在前台显示通知的形式 completionhandler(unnotificationpresentationoptionalert); } @end
推送本地通知
//使用 unnotification 本地通知 +(void)registernotification:(nsinteger )alertime{ // 使用 unusernotificationcenter 来管理通知 unusernotificationcenter* center = [unusernotificationcenter currentnotificationcenter]; //需创建一个包含待通知内容的 unmutablenotificationcontent 对象,注意不是 unnotificationcontent ,此对象为不可变对象。 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]; // 在 alerttime 后推送本地推送 untimeintervalnotificationtrigger* trigger = [untimeintervalnotificationtrigger triggerwithtimeinterval:alertime 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]; }]; }
ios 10 以前本地推送通知:
+ (void)registerlocalnotificationinoldway:(nsinteger)alerttime { // ios8后,需要添加这个注册,才能得到授权 // if ([[uiapplication sharedapplication] respondstoselector:@selector(registerusernotificationsettings:)]) { // uiusernotificationtype type = uiusernotificationtypealert | uiusernotificationtypebadge | uiusernotificationtypesound; // uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:type // categories:nil]; // [[uiapplication sharedapplication] registerusernotificationsettings:settings]; // // 通知重复提示的单位,可以是天、周、月 // } uilocalnotification *notification = [[uilocalnotification alloc] init]; // 设置触发通知的时间 nsdate *firedate = [nsdate datewithtimeintervalsincenow:alerttime]; nslog(@"firedate=%@",firedate); notification.firedate = firedate; // 时区 notification.timezone = [nstimezone defaulttimezone]; // 设置重复的间隔 notification.repeatinterval = kcfcalendarunitsecond; // 通知内容 notification.alertbody = @"该起床了..."; notification.applicationiconbadgenumber = 1; // 通知被触发时播放的声音 notification.soundname = uilocalnotificationdefaultsoundname; // 通知参数 nsdictionary *userdict = [nsdictionary dictionarywithobject:@"开始学习ios开发了" forkey:@"key"]; notification.userinfo = userdict; // ios8后,需要添加这个注册,才能得到授权 if ([[uiapplication sharedapplication] respondstoselector:@selector(registerusernotificationsettings:)]) { uiusernotificationtype type = uiusernotificationtypealert | uiusernotificationtypebadge | uiusernotificationtypesound; uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:type categories:nil]; [[uiapplication sharedapplication] registerusernotificationsettings:settings]; // 通知重复提示的单位,可以是天、周、月 notification.repeatinterval = nscalendarunitday; } else { // 通知重复提示的单位,可以是天、周、月 notification.repeatinterval = nsdaycalendarunit; } // 执行通知注册 [[uiapplication sharedapplication] schedulelocalnotification:notification]; }
本文已被整理到了《ios推送教程》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。