我有一个带有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中保存来自远程通知的信息
发布评论