ios之极光推送消息收到以后对消息的处理总结
当我们的APP收到推送消息后,通常需要根据推送内容点击消息进入到指定的页面
这里讲一下收到推送消息后的处理,分为三种情况 :1.APP处于前台运行情况下
2.APP处于后台挂起情况下
3.APP未启动情况下
前两种相对好处理一点,我是在didReceiveRemoteNotification方法里接受到消息后发一个通知给MainViewController,跳转界面
具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - ( void )application:(UIApplication *)application didReceiveRemoteNotification:( NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler { // Required, iOS 7 Support [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); // NSLog(@"%@",userInfo); _notDic = [ NSMutableDictionary dictionary]; [_notDic setObject:userInfo[@ "ID" ] forKey:@ "myID" ]; [_notDic setObject:userInfo[@ "PICPATH" ] forKey:@ "myPic" ]; //判断应用是在前台还是后台 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { //第一种情况前台运行 NSString *apnCount = userInfo[@ "aps" ][@ "alert" ]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@ "推送信息" message:apnCount delegate: self cancelButtonTitle:@ "查看" otherButtonTitles:@ "取消" , nil ]; alert.delegate = self ; [alert show]; } else { //第二种情况后台挂起时 [[ NSNotificationCenter defaultCenter]postNotificationName:KJPUSHNOT object: nil userInfo:_notDic]; } } |
第三种程序未启动时情况下,需要在didFinishLaunchingWithOptions方法里处理,而不能通过通知跳转页面,因为这时MainViewController还没有走viewDidLoad方法,是没办法接受通知的,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 | - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions { // Override point for customization after application launch. //判断是否是通过点击推送消息进入的APP NSDictionary *resultDic = launchOptions[@ "UIApplicationLaunchOptionsRemoteNotificationKey" ]; if (resultDic) { //推送进入APP self .window.rootViewController = wantVC; } else { //正常进入APP self .window.rootViewController = mainVC; } return YES ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!