Apple Push Notification in Background Issue
Check the following:
- Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
Execute code in the background when push notification is received iOS15
There is a new feature since iOS 15: Location Push Service Extension (CLLocationPushServiceExtension - docs here). It is a power efficient way to query locations on iOS devices, even when your app isn’t running.
Please dive in into the docs and follow steps to configure Xcode project, add a Location Push Service Extension Target and implement Location Push functionality. Don't forget about proper entitlements and to request authorization for Location Services!
https://developer.apple.com/documentation/corelocation/cllocationmanager/creating_a_location_push_service_extension
Apple Push Notification not working properly
1) why didReceiveRemoteNotification: is not working in background
It's ok such behaviour, didReceiveRemoteNotification:
is not called in background. Instead of this, in didFinishLaunchingWithOptions:
, should be checked if app was started because of push notification arrival:
NSDictionary *aPushNotification = [launchOptions valueForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(aPushNotification) {
// Do something with push's payload
}
2) why notification's banner not showed when app is in background
This happened because 'alert' field was missing from push notification's payload
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
Related Topics
Error: Uitableview Jump to Top with Uitableviewautomaticdimension
How to Parse Array of JSON to Array in Swift
Cells Order in Uicollectionview
Programmatically Access Image Assets
Onfocuschange Not Triggered in Swiftui Tvos
Error Setting Text in Collection View Cell
Supporting Multiple Screen Sizes in Storyboards (3.5 and 4 Inch Screens)
Skspritenode Position Changes to 0,0 for No Reason
Swiftui: Animation Inside Navigationview
Swift - Get Date from Day of Year
Customizing Uilocalnotification's Alert
I Added a Localization in Xcode, Removed It and Now My Storyboard Is Gone
Uitableview Strange Layout Behavior Changes on Scroll
Stroke Width with a Scenekit Line Primitive Type
How to Do Authorization and Login with Wechat Using the iOS Sdk