7402

About save in formation from remote notification in ios

Question:

I have a push notification with struct

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

I want to store value of key <strong>"funcId"</strong> 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]; }

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

Thanks all support!

Answer1:

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 <strong>userInfo</strong> is <strong>not nil</strong> before saving the value/object into NSUserDefaults.

The code is something like below:-

//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]; } }

Answer2:

You cannot save to NSUserDefaults, since the you don't have the permission.

Create another file using NSFileManager, and set the proper file permission.

Then you'll be able to write to that file when receiving notification.

Recommend

  • block execution iOS and assigning variable
  • Unable to send Multipart-Form data with AFNetworking 3.0
  • Share Facebook User ID between more than one application
  • How to check for a badge number on app launch on iPhone
  • Building for iOS if registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
  • IOS : com.facebook.sdk error 5
  • NSUserDefaults standardUserDefaults setObject: forKey: not working for Multivalue preference
  • Payumoney getway error occur in ios
  • Is Cocoa's NSMutableArray sparse?
  • Why does adding my viewcontroller to a navigationcontroller seem to interfere with my constraints?
  • Storing access token and refresh token in KeyChain
  • Getting information from a NSDictionary
  • How to create sharable Core Data .sqlite backups locally and in iCloud using current NSPersistentSto
  • Swift POST Request in same Thread
  • iOS how to store a NSDictionary or JSON in a file's xattr?
  • Xcode6 - Autolayout view in an other autolayout view
  • UserDefaults between Objective-C and Swift
  • Post on Facebook Page from iOS App Not Showing Up on Page?
  • complex JSON parsing using Objective-C
  • How to convert a File Blob to NSData?
  • Notifications causing no dealloc to be called
  • Using iPhone as iBeacon Transmitter
  • JSON validation
  • How to observe the value of an NSTextField
  • How to Pass information Back in iOS when reversing a Gesture Recognizer using Swift?
  • objective c when to use NSDictionary instead of NSArray [duplicate]
  • Swipe Buttons in IOS 9 Remote Notification - Work when app in background not when app out of memory
  • 'Terminating app due to uncaught exception'.. it couldn't find MainWindow, which no l
  • NSDate isMemberOfClass: [NSDate class] returns false? [duplicate]
  • Chrome identity api to get profile info
  • How to execute two asynchronous functions sequentially
  • Checking launchOptions in Swift 3
  • Help with SimpleCursorAdapter and Listview
  • Stored data can't access in Yii
  • UICollectionview scroll horizontal and vertical
  • Encoding issue with ASIHttpRequest
  • didUpdatePushCredentials not get called
  • Build Successful but not running on simulator
  • How to change the font size of a single index for UISegmentedControl?
  • QLPreviewController hide print button in ios6