'when tapped notification willPresent notification: is not called

I'm using UNNotificationExtension and after using it the remote call function is never called. I'm trying to get the data whenever notification is tapped.

@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
   
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        didReceive response: UNNotificationResponse,
                                        withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        completionHandler()
    }

the call to this function is never received. What i can do so i can get data whenever user clicks the notification either foreground or background.



Solution 1:[1]

As no information is given regarding whether didReceive(_:withContentHandler:) method which is present inside Notification extension folder is called or not in your case, I presume it is not getting called. As that method needs to be handled correctly inorder to execute userNotificationCenter(_ center: UNUserNotificationCenter,didReceive: .. method.

Once remote notification for your app is received, the system loads your extension and calls its didReceive(_:withContentHandler:) method only when both of the following conditions are met:

  • The remote notification is configured to display an alert.
  • Remote notification’s payload includes the mutable-content key with
    the value set to 1.

And call the contentHandler closure with passing the bestAttemptContent. Once this is done the userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response will be called.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharasis Behera