Home / IOS Development / How to handle notification before user presses it? : iOS Programming

How to handle notification before user presses it? : iOS Programming



I am very confused about searching online for handling push notifications on iOS. AFAIK if you appoint a delegate UNNotificationCenter then the method userNotificationCenter(_:didReceive:withCompletionHandler:) is called when the user presses the alert. What’s more, if you use silent alerts, then the method application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`Kalles. But it does not suit my needs, because I want to be notified before the user has the chance to press alert and update my data in the background, so that the user will see the “latest”

; data immediately.

I saw several places that I do not want to send delegates to UNNotificationCenter then application(_:didReceiveRemoteNotification:fetchCompletionHandler:) called instead, even twice, but it does not work for me: https://stackoverflow.com/questions/64172345/application-didreceiveremotenotificationfetchcompletionhandler-is-not-called.

Does this mean that it is not possible to handle the notification before the user presses it? Is it a good practice to always send two notifications at the same time: one notification (so that the user will see the notification list) and one silent (so that the app can update data)?

EDIT

Interestingly, after installing the app several times, it started working. I put in UNNotificationCenterdelegate and it works like this:

  1. The app is in the background, the server sends a message with content-available: 1.

  2. The system displays alarm and application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is called so I can retrieve new data.

  3. User cranes on standby, userNotificationCenter(_:didReceive:withCompletionHandler:) called so I can navigate to a specific view.

Is there a proper way to handle push alerts with data payload?


Source link