Receiving Push Notifications While in Background

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

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

Receiving Push Notifications while in background

From the APNS programming guide :

Let’s review the possible scenarios when the operating delivers a
local notification or a remote notification for an application.

The notification is delivered when the application isn’t running in the
foreground. In this case, the system presents the notification,
displaying an alert, badging an icon, perhaps playing a sound.

As a result of the presented notification, the user taps the action button
of the alert or taps (or clicks) the application icon. If the action
button is tapped (on a device running iOS), the system launches the
application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it
passes in the notification payload (for remote notifications) or the
local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application
calls the same method, but furnishes no information about the
notification
.

I believe the last sentence describes your case, and explains why your application gets no information about the notification.

Get push notification while App in foreground iOS

If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification as follows

  • Check whether application is in active state on receiving a remote

    notification. If in active state fire a UILocalNotification.

    if (application.applicationState == UIApplicationStateActive ) {

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.userInfo = userInfo;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertBody = message;
    localNotification.fireDate = [NSDate date];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

SWIFT:

if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}


Related Topics



Leave a reply



Submit