7

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):

  1. 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?
  2. 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

NightFuryLxD
  • 847
  • 5
  • 15

1 Answers1

0

The answers I've got so far:

  1. I've not been able to simulate background notifications by dropping the .apns file into the simulator. It should be possible to go via APNS to simulator if you have a silicon mac (as linked in the question).
  2. After force-quitting the app by swiping it form the app-switcher, I've observed that sometimes, Background Pushes work and sometimes they don't (I've posted about this ambiguous behavior here)... but as officially documented, it is not supposed to work, so let's go with that. Recently, in the latest versions of iOS (16.4 as of this writing), Background Pushes are not working after user has swiped app from the app-switcher... so maybe this was a bug and was addressed in latest updates.

Do let me know if someone has a different observation.

Update (from comments): Even with a silicon Mac, it is observed that background push doesn't work. Alert notifications work though.

NightFuryLxD
  • 847
  • 5
  • 15
  • "It should be possible to go via APNS to simulator if you have a silicon mac" is this true for you? I can get it to receive alerts on simulator, but not background messages – urSus Aug 09 '23 at 17:25
  • @urSus, I haven't tested that... because I don't have a silicon mac. According to [this post](https://stackoverflow.com/questions/1080556/how-can-i-test-apple-push-notification-service-without-an-iphone/73829063#73829063), remote notifications (which includes background pushes) should work. – NightFuryLxD Aug 12 '23 at 03:58
  • 1
    I have the mac and only the alerts work, backgroud don't, they have a bug, as usual – urSus Aug 15 '23 at 22:15