Sebastien
Sebastien

Reputation: 4295

How to debug background notifications in iOS?

I am trying to debug background push notifications on iOS, and more specifically to set breakpoints into code that should handle a notification to configure internationalized content using custom logic.

I set a breakpoints in my AppDelegate's userNotificationCenter(_:willPresent:withCompletionHandler:) and userNotificationCenter(_:didReceive:withCompletionHandler:).

And I also created a notification service extension and added a breakpoint to its didReceive(_:withContentHandler:) because I want to mutate the content of my notification.

When I send the following payload and my app is in the foreground, the breakpoint in userNotificationCenter(_:willPresent:withCompletionHandler:) gets a hit and everything is fine:

{
  "aps" : {
    "badge" : 1012,
    "category" : "GENERIC_MESSAGE",
    "mutable-content" : 1
  },
  "translations" : [
    {
      "LanguageCode" : "fr",
      "Title" : "Aimant détaché",
      "Body" : "L'aimant du capteur 002F51AB, associé à l'outil HT-1225 Hitachi Tas, a été détaché."
    },
    {
      "LanguageCode" : "en",
      "Body" : "The magnet of the tag 002F51AB, link to tool HT-1225 Hitachi Tas, was detached.",
      "Title" : "Magnet detached"
    },
    {
      "LanguageCode" : "nl",
      "Body" : "De magneet van de tag 002F51AB, gelinkt aan het gereedschap HT-1225 Hitachi Tas, is losgekoppeld.",
      "Title" : "Magneet losgekoppeld"
    }
  ],
  "messageId" : "90073ebb-ce51-ea11-a94c-000d3a213771"
}

But if the application is in the background and I send the exact same notification, I don't get a hit on userNotificationCenter(_:didReceive:withCompletionHandler:). Same thing when I run the notification service extension, I don't get a hit in didReceive(_:withContentHandler:)

Am I forgetting something? Is it because my notification payload doesn't have an aps alert field?

Upvotes: 0

Views: 1887

Answers (1)

Sebastien
Sebastien

Reputation: 4295

I figured it out: when I add an alert field to my push notification, even with a dummy string, then it get forwarded to my Notification Service. Otherwise it doesn't.

Upvotes: 1

Related Questions