I want to insert the data received from the push notification into the database when the app is not open or inactive. Is there any way in which I can save all the received push notifications into my database when the app is not open or inactive?
-
possible duplicate of [Receiving Push Notifications while in background](http://stackoverflow.com/questions/14616261/receiving-push-notifications-while-in-background) – Lars Blumberg Feb 20 '15 at 13:38
4 Answers
I suggest what you do and what may be possible is to add methods (saving info in the database here) in this AppDelegate method.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
when you receive a notification you can handle what you want to happen here. The NSDictionary value being passed in stores all your push's information and you get their values by doing something like this
//Your dictionary may be set up a bit differently but that's okay
[userInfo objectForKey:@"alert"];
this contains the message or "alert" message in your push notification. You can use that key and store it in a NSString
instance and save it into the database (however that works for you). I am using Parse.com for my backend so I would do something like so to save an object in the background.
PFObject *someObject = [PFObject objectWithClassName:<Class Name>];
messageObject[@"message"] = [userInfo objectForKey:@"alert"];
[someObject saveInBackground];
I have not actually tried this but I am assuming it would work

- 734
- 15
- 25
You need to check the property applicationState in the application object when receiving notification.
UIApplicationState applicationState = [application applicationState];
It can be UIApplicationStateActive, UIApplicationStateInactive or UIApplicationStateBackground.
Hope this helps ;-)

- 170
- 1
- 1
- 11
Always you receive a Notification, it goes directly to the callback method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Get info to save from userInfo dictionary
// Save it with Core Data, Archiving, NSUserDefaults, SQLite...
}
Independently it comes from Background
or Foreground
. You just have to get the info you need of the Notification from the userInfo
Dictionary
.
As well here you can check the state of your app with UIApplicationState
. That gives you the chance of split the logic of Background/Foreground states, if you want to show for example an UIAlertView
when your app is on the Foreground
. Or not, if you are in the Background
.

- 825
- 1
- 11
- 26
Using Notification service extension
, we can store data into database.
a) Add Notification service extension
using +
icon in bottom
b) Create provision profiles for this
c) Open newly create target and open NotificationService.swift
file
d) Handle notification using below method
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// print("Notification service extension user info :", bestAttemptContent?.userInfo)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
// bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
if let aps = bestAttemptContent.userInfo["aps"] as? NSDictionary,
let data = aps["data"] as? NSDictionary {
saveNotificationIntoDB(data: data)
}
contentHandler(bestAttemptContent)
}
}

- 812
- 6
- 16