While sending a push notification in 2nd step, toggle Advanced, then click on Silent for a notification without sound, badge or message.
--OR--
API
You can set silent_push
to true
to payload if sending from API
{"payload":{"silent_push":true}}
And for content-available field
{"payload":{"contentAvailable":true}}
2. Enable background fetch and remote notifications and background fetch in background modes under capabilities:
3. Place your code in didReceiveRemoteNotification:fetchCompletionHandler in AppDelegate:
Use this method to process incoming remote notifications for your app. The system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
As soon as you finish processing the notification, you must call the block in the handler
parameter or your app will be terminated. Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice, you should call the handler block as soon as you are done processing the notification. The system tracks the elapsed time, power usage, and data costs for your app’s background downloads. Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future notifications.
Objective-c
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"application:didReceiveRemoteNotification:fetchCompletionHandler: %@", userInfo);
completionHandler(UIBackgroundFetchResultNoData);
}
Swift
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("application:didReceiveRemoteNotification:fetchCompletionHandler: \(userInfo)")
completionHandler(.newData)
}
Please make sure background App fetch is enabled in your iOS testing device, while testing the background notificaiton processing while application is force quitted in inactive mode