Apple Push Notification in Background Issue

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



Leave a reply



Submit