I've been studying background execution in iOS. One of the methods is silent notifications.
Silent notifications (aka background push) can be used to wake the app from suspended state and update content. From Xcode 11.4, it is possible to simulate regular user notifications, as described here. Then, from Xcode 14 onwards, those with Apple silicon Mac laptops will be able to send notifications to simulator from APNS itself... wonderful.
But this post is about silent notifications.
.apns file used is,
{
"Simulator Target Bundle": "com.example.IOSBackgroundExec",
"aps": {
"content-available": 1
}
}
In project settings, 'remote notification' is enabled under 'Signing and Capabilities' tab and Push notification is added. User permission is not required for silent notifications.
The following delegate method is supposed to be invoked when app is woken up.
// Handling silent remote notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NSLog(AppDelegate.TAG + "application(_:didReceiveRemoteNotification:fetchCompletionHandler:)")
// Some work
completionHandler(.newData)
}
Question(s):
- When I drag and drop the .apns file, the delegate method is not invoked. I'm aware that Background Fetch and Background Processing cannot be tested in simulator because, their scheduling is dependent on system conditions, which is beyond the simulator. So silent notifications are also not possible to test in simulator?
- According to documentation, by user force-quit, user swiping app from app switcher is referred? Even after swiping WhatsApp, you still get notified of incoming messages as long as there's an internet connection, right?
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.
Environment: Xcode 14.2, iOS Simulator 16.x