How to handle Push Notifications that won't open (iOS, Swift)

So, I launch Swift app and use it Parse

to handle my push notifications. So far, everything works as expected.

I am sending objects JSON

via push notifications and updating a local array from the data received. I am processing data via application: didReceiveRemoteNotification

and posting a dictionary via userInfo

.

This works great when the app is open in the foreground. It also works great when the app is in the background and the user opens Push Push when it is displayed on the banner.

The JSON object is processed according to the code application: didReceiveRemoteNotification

.

However, when the user doesn't open the notification, it application: didReceiveRemoteNotification

doesn't get triggered and my local array is not updated.

How can I make sure that every time a notification is sent, the JSON object is processed and updates the local array accordingly?

+3


source to share


3 answers


Generally, the wrong approach is that you shouldn't rely on push notifications, the user may turn them off, or for some reason they cannot be delivered. Instead, you should always receive new data from your server. Read the Apple Push Notification Serving Guide :



Important . Delivery of notifications is a "best effort", not guaranteed. It is not intended to deliver data to your application, but only to notify the user when new data is available.

+2


source


You cannot receive a response or JSON object from a Notification until the user clicks on it and calls the didReceiveRemoteNotification

or method didFinishLaunchingWithOptions

.

From ios8, you can set an interactive notification, which is useful for interacting with the notification directly, without opening the app in the foreground.



HERE is a tutorial for interactive notification.

Help you.

+4


source


application:didReceiveRemoteNotification:fetchCompletionHandler: 

      

this method is called even if the user will not click or interact with the notification.

When a remote notification arrives, the system calls the application:didReceiveRemoteNotification:fetchCompletionHandler:

 method. Notifications usually signal the availability of new information. In the application delegate method, you can start loading new data from the server to update the data structures of your applications. You can also use notification to update your UI. -Apple Inc.

Application state must be in UIApplicationStateActive

or UIApplicationStateBackground

.

There is nothing you can do if the state of the application UIApplicationStateInactive

.

+1


source







All Articles