iOS 本地推送通知和接收通知及其跳转控制总结
程序员文章站
2022-05-12 17:03:35
ios 本地推送通知和接收通知及其跳转控制总结。
1. 本地通知 ios8.0
1.1. 本地通知发送
***ios8.0之后才能用
//本地通知
uilocalnotifi...
ios 本地推送通知和接收通知及其跳转控制总结。
1. 本地通知 ios8.0
1.1. 本地通知发送
***ios8.0之后才能用 //本地通知 uilocalnotification *locationno = [[uilocalnotification alloc] init]; //触发时间 locationno.firedate = [nsdate datewithtimeintervalsincenow:10]; //通知内容 locationno.alertbody = @"这是本地通知"; //ios8.2之后可见,一般不用 if (@available(ios 8.2,*)) { locationno.alerttitle = @"哈哈,本地推送"; } //锁屏(黑屏状态下,"滑动来"后面的字)--ios10.0之后,没有滑动解锁功能了,这个属性也就不存在了 locationno.alertaction = @"查看这个本地推送"; locationno.hasaction = yes; //通知过来时的声音 locationno.soundname = uilocalnotificationdefaultsoundname; //应用图标提示--默认是0,没有改变,设置为负数,通知到达时图标提示就会消失 locationno.applicationiconbadgenumber = -1; //直接设置应用图标提示为0,代表隐藏,和上面效果一致 [uiapplication sharedapplication].applicationiconbadgenumber = 0; //mark: 额外信息--对于用户不重要,对于开发者很重要,用于接收通知时处理相应逻辑 locationno.userinfo = @{@"name":@"周玉",@"job":@"ios开发工程师"}; //加入到调度池---有可能有多个通知---取决于触发时间 [[uiapplication sharedapplication] schedulelocalnotification:locationno];
1.2. 本地通知接收
//#import "appdelegate.h" 注册通知 - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { /* uiusernotificationtypenone = 0, uiusernotificationtypebadge 徽章标记 uiusernotificationtypesound 声音 uiusernotificationtypealert 弹出效果 */ //注册通知 //本地通知设置 uiusernotificationsettings *setting = [uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge | uiusernotificationtypesound | uiusernotificationtypealert categories:nil]; [[uiapplication sharedapplication] registerusernotificationsettings:setting]; //当程序被杀死的情况下,接收到通知并执行事情-- uilocalnotification *notification = launchoptions[uiapplicationlaunchoptionslocalnotificationkey]; if (notification) { nslog(@"localno = %@",notification.userinfo);//nslog不会再打印 //用测测试是否接收到通知 uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(100, 100, 100, 100)]; label.backgroundcolor = [uicolor redcolor]; [self.window.rootviewcontroller.view addsubview:label]; return yes; }
// 程序没有被杀死时,接收到本地通知时调用--点击通知时会执行这个方法,如果程序被杀死时,这个方法不再走,在application: didfinishlaunchingwithoptions:方法中获取 - (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification{ nslog(@"notification.userinfo = %@",notification.userinfo);
//锁屏(黑屏状态下,”滑动来”后面的字)–ios10.0之后,没有滑动解锁功能了,这个属性也就不存在了
locationno.alertaction = @”查看这个本地推送”;
locationno.hasaction = yes;
2. 本地通知逻辑处理 ios8.0
2.1. 发送推送通知
// 发送通知 // viewcontroller.m // 本地推送 // // created by zhouyu on 2017/12/27. // copyright ? 2017年 zhouyu. all rights reserved. // #import "viewcontroller.h" #import "redcontroller.h" #import "bluecontroller.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.title = @"本地推送"; [self setupui]; } - (void)setupui{ uibutton *btn1 = [[uibutton alloc] initwithframe:cgrectmake(100, 300, 150, 60)]; [btn1 settitle:@"跳转红色控制器" forstate:uicontrolstatenormal]; [btn1 settitlecolor:[uicolor redcolor] forstate:uicontrolstatenormal]; [btn1 addtarget:self action:@selector(red) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn1]; uibutton *btn2 = [[uibutton alloc] initwithframe:cgrectmake(100, 400, 150, 60)]; [btn2 settitle:@"跳转蓝色控制器" forstate:uicontrolstatenormal]; [btn2 settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal]; [btn2 addtarget:self action:@selector(blue) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn2]; } - (void)red{ [self pushlocationnotificationwithalertbody:@"跳转红色控制器" userinfo:@{@"key":@"red"}]; } - (void)blue{ [self pushlocationnotificationwithalertbody:@"跳转蓝色控制器" userinfo:@{@"key":@"blue"}]; } - (void)pushlocationnotificationwithalertbody:(nsstring *)body userinfo:(nsdictionary *)userinfo{ //本地通知 uilocalnotification *locationno = [[uilocalnotification alloc] init]; //触发时间 locationno.firedate = [nsdate datewithtimeintervalsincenow:5]; //通知内容 locationno.alertbody = body; //通知过来时的声音 locationno.soundname = uilocalnotificationdefaultsoundname; //应用图标提示--默认是0,没有改变,设置为负数,通知到达时图标提示就会消失 locationno.applicationiconbadgenumber = -1; //直接设置应用图标提示为0,代表隐藏,和上面效果一致 [uiapplication sharedapplication].applicationiconbadgenumber = 0; //mark: 额外信息--对于用户不重要,对于开发者很重要 locationno.userinfo = userinfo; //加入到调度池---有可能有多个通知---取决于触发时间 [[uiapplication sharedapplication] schedulelocalnotification:locationno]; } @end
2.2. appdelegate接收推送通知进行逻辑处理
// 接收通知并进行逻辑处理 // appdelegate.m // 本地推送 // // created by zhouyu on 2017/12/27. // copyright ? 2017年 zhouyu. all rights reserved. // #import "appdelegate.h" #import "viewcontroller.h" #import "redcontroller.h" #import "bluecontroller.h" @interface appdelegate () @end @implementation appdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.window = [[uiwindow alloc] initwithframe:[uiscreen mainscreen].bounds]; self.window.rootviewcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:[[viewcontroller alloc] init]]; [self.window makekeyandvisible]; self.window.backgroundcolor = [uicolor whitecolor]; /* uiusernotificationtypenone = 0, uiusernotificationtypebadge 徽章标记 uiusernotificationtypesound 声音 uiusernotificationtypealert 弹出效果 */ //注册通知 //本地通知设置 uiusernotificationsettings *setting = [uiusernotificationsettings settingsfortypes:uiusernotificationtypebadge | uiusernotificationtypesound | uiusernotificationtypealert categories:nil]; [[uiapplication sharedapplication] registerusernotificationsettings:setting]; //当程序被杀死的情况下,如何接收到通知并执行事情--ios10.0之后废弃,需要用10.0之前版本测试 uilocalnotification *notification = launchoptions[uiapplicationlaunchoptionslocalnotificationkey]; if (notification) { nslog(@"localno = %@",notification.userinfo);//nslog不会再打印 uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(100, 100, 100, 100)]; label.backgroundcolor = [uicolor redcolor]; [self.window.rootviewcontroller.view addsubview:label]; [self jumptocontrollerwithlocationnotification:notification]; } return yes; } // 程序没有被杀死时,接收到本地通知时调用--点击通知时会执行这个方法,如果程序被杀死时,这个方法不再走,在application: didfinishlaunchingwithoptions:方法中获取 - (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification{ nslog(@"notification.userinfo = %@",notification.userinfo); [self jumptocontrollerwithlocationnotification:notification]; } - (void)jumptocontrollerwithlocationnotification:(uilocalnotification *)localno{ //如果app在前台,就不用走通知的方法了 if ([uiapplication sharedapplication].applicationstate == uiapplicationstateactive) { return; } //获取userinfo nsdictionary *userinfo = localno.userinfo; uinavigationcontroller *nav = (uinavigationcontroller *)self.window.rootviewcontroller; //判断跳转 if ([userinfo[@"key"] isequaltostring:@"red"]) { [nav pushviewcontroller:[[redcontroller alloc] init] animated:yes]; } else if([userinfo[@"key"] isequaltostring:@"blue"]) { [nav pushviewcontroller:[[bluecontroller alloc] init] animated:yes]; } } @end
//如果app在前台,就不用走通知的方法了
if ([uiapplication sharedapplication].applicationstate == uiapplicationstateactive) {
return;
}
上一篇: 如此吵闹的车厢
下一篇: 快速开发平台能带来什么价值