关于在ios中保存来自远程通知的信息

编程入门 行业动态 更新时间:2024-10-09 05:12:32
本文介绍了关于在ios中保存来自远程通知的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有struct的推送通知

I have a push notification with struct

{ "aps": { "alert": "Hello, world!", "sound": "default", "funcId": "abc" <-- this key i add more } }

我想存储键"funcId" 的值,以便在应用收到通知(状态为已取消或已终止的应用)后使用,例如:

I want to store value of key "funcId" to use after app recives notification (app in state backgound or killed) like:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"]; [[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification]; [[NSUserDefaults standardUserDefaults] synchronize]; }

我曾尝试使用NSUserDefaults来存储此值,但是当应用程序处于状态后台并杀死了该值时,不会存储该值. 如何存储从远程通知中获取的信息以在应用重新启动时使用?

I had try use NSUserDefaults to store this value, but when app in state background and killed this value not store. How to store vaule from remote notification to use when app relaunch?

感谢所有支持!

推荐答案

您将必须使用2种不同的方法来处理Push Notification,因此,您将必须用2种不同的方法来保存值.在将值/对象保存到NSUserDefaults之前,还需要确保 userInfo 为 nil .

You will have to handle Push Notification in 2 different methods, so, you will have to save the value in 2 different methods. You will also need to make sure that the userInfo is not nil before saving the value/object into NSUserDefaults.

代码如下:-

//Receive Push Notification when the app is active in foreground or background - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if(userInfo){ //TODO: Handle the userInfo here NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"]; [[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification]; [[NSUserDefaults standardUserDefaults] synchronize]; } } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Get the push notification when app is not open NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; if(remoteNotif){ [self handleRemoteNotification:application userInfo:remoteNotif]; } return YES; } -(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{ if(userInfo){ //TODO: Handle the userInfo here NSString *sFuncID = [[userInfo objectForKey:@"aps"] objectForKey:@"funcId"]; [[NSUserDefaults standardUserDefaults] setValue:sFuncID forKey:Key_ID_notification]; [[NSUserDefaults standardUserDefaults] synchronize]; } }

更多推荐

关于在ios中保存来自远程通知的信息

本文发布于:2023-11-25 18:05:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   信息   ios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!