On iOS, how can my app get the notification data that it received while the phone was in the background?
As per apple documentation:
When a remote notification arrives, the system calls the application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Notifications usually signal the availability of new information. In your app delegate method, you might begin downloading new data from a server so that you can update your app’s data structures. You might also use the notification to update your user interface.
You have to enable remote notification background mode
, only then you will get a callback to the above mentioned method.
iOS Handle notifications and payload when app is in Background
There are two types of push notifications, alert notifications and background notifications. Alert notifications allow you to deliver visible alerts that can be interacted with in ways that your app can customize.Background notifications allow your application to fetch data from the background, upon receiving push notifications. Background notification should be used to keep your application up to date, even if the application isn't running. Also as of ios 10(and above)instead of using the didReceiveRemoteNotification method you can use didReceive method for handling the alert notifications.
Now coming back to your question in case of the alert notification the didReceive/didReceiveRemoteNotification method is called when the application is in the foreground or when the user taps on the application. Since, you want to update the database you can use the background notifications instead of the alert notification as it will automatically raise your application even when it is in background and will also call the didReceiveRemoteNotification:fetchCompletionHandler. while sending a background push notification make sure you :
- Edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes.
- Add "content-available":1 to your push notification payload, otherwise the app won't be woken if it's in the background
- The notification’s POST request should contain the apns-push-type header field with a value of background, and the apns-priority field with a value of 5.
For more info please refer :
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app
iOS push notification: how to detect if the user tapped on notification when the app is in background?
OK I finally figured out.
In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification:
will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.
If you uncheck that box, application:didReceiveRemoteNotification:
will be triggered only when you tap on the notification.
It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.
Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.
how to perform action on receive push notification when app is in background?
I know of no way to handle push notifications even if your app is not in active state (And I think that's exactly the it should be)
You should check for PNS in your AppDelegate's didFinishLaunch as well
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//...
//If Push Notification
NSDictionary *pnsDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pnsDict != nil){
DDLogInfo(@"PNS");
[self handlePushNotification:pnsDict];
}
//..
}
This way you can be sure that you catch all remote notifications.
Be aware, that your app should not depend on pns
. PNS maybe fail to be delivered or the user can turn them off. The application should always work with pns enabled as well as pns disabled in the same way.
For more information read the Apple PNS Guide
Related Topics
How to Get the Index (String.Index) Value from the Cursor Position of a Uitextview Element in Swift
How to Create a "Hyperlink" with Swift
Tableview with Searchcontroller - Deinit Not Called
How to Add the Same Background Image to All Views in My iOS App Using Swift
How to Call the Apple Wallet from iOS App Using Swift
Get the Exact Difference Between 2 Dates for a Single Nsdatecomponent
Swift3: Live Uilabel Update on User Input
Coretelephony Esim Functions Not Working on Device
iOS PDFkit Displaymode = Singlepage Only Shows the First Page of the PDF
Longer Subtitles in Mapview Annotations (Swift)
Changing Constraint When Keyboard Appears - Swift
Scrolling in Uicollectionview Selects Wrongs Cells - Swift
Get Section Number in Custom Cell Button Action
Ios11 Uibarbuttonitem Not Working
Uitextview as Inputaccessoryview Doesn't Render Text Until After Animation