Push notifications functions never triggered

I've spent over a day on this so I'm hoping someone on here has some idea as to what is going on for me. I've implemented push notifications in my app, but when I send a notification to a device, none of the receiving function are triggered.

When the app is in the background, the notification arrives correctly, and when tapped it correctly launches the app - but the didReceiveRemoteNotification function is never triggered, so I'm unable to handle/react to the notification.

When the app is in the foreground, neither the willLaunch or didReceive functions are triggered. I'm setting the UNUserNotificationCenterDelegate in the willFinishLaunchingWithOptions function, and when I monitor the app via the console, it shows no errors and shows willPresentNotification delivery succeeded

I have updated the Background Modes to include background fetch, background processing, and remote notifications. I have also tested this on both the Xcode simulator and a physical device with the same results. I've also tested this with push notifications coming from my server, push notifications coming from the CloudKit console, and just dropping a JSON file onto the simulator. All resulted in the same behaviour.

Does anyone have any idea why these functions might not be triggered??

Does your app calls registerForRemoteNotifications() when launched?

I am going to guess that you might have some misunderstandings and misplaced expectations about what notifications functions are called when, and about an app's lifecycle in general. For example, expecting a call to didReceiveRemoteNotification when the notification is tapped, or expecting a call to willFinishLaunching when the app is in the foreground.

I will suggest to read the article Handling notifications and notification-related actions thoroughly to understand your notification handling options.

Also, as there might be some additional missed expectations for what happens when the app is launched vs when it is already running, the articles Managing your app’s life cycle and Responding to the launch of your app should clarify things.

To address the issues you brought up specifically:

  • didReceiveRemoteNotification has two versions, one of them long deprecated. Make sure you are using application(_:didReceiveRemoteNotification:fetchCompletionHandler:) and not the deprecated version
  • also, this function will only be triggered if your push payload contains content-available:1, and even then it is not guaranteed to be called every single time. Whether this function will be called or not is at the discretion of the system, and you can only expect 1-2 callbacks per hour (still not guaranteed)
  • application(_:willFinishLaunchingWithOptions:) will only be called when your app is not already running. Definitely will not be called when it is already in the foreground like you say
  • userNotificationCenter(_:didReceive:withCompletionHandler:) is expected to be called when the user interacts with the notification. Make sure your function signature is correct, your delegates are set correctly, and they have not been changed or reinitialized between app launch and the notification being interacted with.

Hopefully all this information will give you a hand in locating where the problem lies.


Argun Tekant /  DTS Engineer / Core Technologies

Thank you for responding for quickly - I agree that I've almost certainly misunderstood how the notifications system works, I'm just struggling to figure out where that misunderstanding lies.

I'll give a bit more info just so we're on the same page:

  • I can confirm that I'm definitely using the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) function
  • I have the content-available:1 in the payload (see below the test payload I'm using)
  • The function application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is never called - I've attempted >50 tests with both the CloudKit console and dropping the JSON payload onto the simulator and I've never had this function trigger - is this something that is expected? From your wording I would expect it to be triggered at least some of those times, but it appears to be consistent so far
  • I'm only using the application(_:willFinishLaunchingWithOptions) function to set the delegate for the UNUserNotificationCenter
  • The launch function I was referring to is the userNotificationCenter(_ :willPresent:withCompletionHandler) function - this function is never triggered when the app is in the foreground, even though my console that is attached reads willPresentNotification delivery succeeded`

This is the payload I'm using to test this functionality - could it be something wrong in the payload itself that could cause the notification to not be delivered to the app?

{
  "aps": {
    "content-available": 1,
    "alert": {
      "title": "Test Push",
      "body": "Success! Push notification in simulator! 🎉",
      "sound": "default"
    }
  }
}

Thanks again!

Push notifications functions never triggered
 
 
Q