I am trying to save a data that comes with push notification payload. It works well when the app is running but not when the app is closed.
how can I save data from
I don't save them into CoreData but I save them into NSUserDefaults. This solution was recommended by Apple Team Support and it is only for iOS 10, I implemented it and it works well! I leave the link:
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ModifyingNotifications.html
This is the procedure:
NOTE: Consider that you will use three identifiers: 1) Identifier for your app (you already have) 2) Identifier for your extension (you will create) 3) Identifier for App Group. (you will create)
Is necessary to enable App Groups to create a resource where you can save and read info for your app and the extension. Click on "Show Project Navigator". Into the list of targets select your main project. Then, click on "Capabilities" and switch on the option named "App Groups". Please add the identifier for App Group to identify share resources. You should do the same step for the target of extension that you created (Select target of extension - Capabilities - Switch on at "App Groups" - Add the same identifier for App Group)
You should to add the Identifier for your extension into Apple Developer Site for identify the Notification Service Extension, also you should to make new Provisional Profiles (Development, AdHoc and/or Production) and associate it with the new Provisional Profiles.
On both identifiers (App and Extension) you should edit them and enable "App Groups" Service in both of them. You should to add the Identifier for App group into the App Groups Services.
NOTE: Identifier for App and Identifier for Extension SHOULD HAVE THE SAME IDENTIFIER FOR APP GROUP.
Download the new Provisional Profile on Xcode and associate them with your Notification Service Extension. Please ensure you everything is ok with them.
After that, into "Capabilities" of your app and extension, open "App Groups" section and update them. The three steps - 1)Add the App Groups entitlements to your entitlements file, 2) App the App Groups feature to your App ID and 3) Add App Groups to your App ID - should be checked.
Come back to Project navigator and select the folder of you extension. Open the .m file. You will see a method called didReceiveNotificationRequest:(UNNotificationRequest *)request. Into this method you will create a different NSUserDefaults with SuiteName exactly equal to the Identifier for app group like this:
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"identifier for app group"];
Into this same method, get the body of the notification and save it into a NSMutableArray, then save it in the share resources. Like this:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{ self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; NSMutableArray *notifications = [NSMutableArray new]; NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"]; notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy]; if (notifications != nil){ [notifications addObject:self.bestAttemptContent.userInfo]; }else{ notifications = [NSMutableArray new]; [notifications addObject:self.bestAttemptContent.userInfo]; } [defaultsGroup setObject:notifications forKey:@"notifications"]; }
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"]; NSMutableArray *notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];
I hope to be clear with each step. I going to write a post to implement this solution with images in my page. Another point that you should consider is it doesn't work with Silent Push Notification.
The following is taken from the apple documentation...
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
Note the highlighted text, the app will not be started if it is completely closed