简体   繁体   中英

How to Handle Push Notifications that are not Opened (iOS, Swift)

So I am running a Swift application and using Parse to handle my push notifications. So far everything is working as expected.

I am sending JSON objects through the push notifications and updating a local array from the data received. I am handling the data through the application: didReceiveRemoteNotification and sending a dictionary through userInfo .

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

The JSON object is handled according to the application: didReceiveRemoteNotification code.

However, when the user does not open the notification, application: didReceiveRemoteNotification does not get run and my local array does not get updated.

How do I make sure every time a notification is sent the JSON object gets handled and updates my local array accordingly?

You can not get get response or JSON object from Notification until User click on it and either didReceiveRemoteNotification or didFinishLaunchingWithOptions method get invoke.

From ios8, you can set interactive notification, which is useful to interact direct from notification, without opening application in foreground.

HERE it the tutorial for interactive Notification.

May this help you.

It is generally incorrect approach, you should not rely on push notifications, user may switch them off or due to some reasons they may not be delivered. Instead you should get new data always from your server. Read from the Apple Push Notification Service Guide :

Important : Delivery of notifications is a “best effort”, not guaranteed. It is not intended to deliver data to your app, only to notify the user that there is new data available

application:didReceiveRemoteNotification:fetchCompletionHandler: 

this method will call even if user will not tap or interact with notification.

When a remote notification arrives, the system calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method. Notifications usually signal the availability of new information. In your app delegate method, you might begin downloading new data from a server so that you can update your app's data structures. You might also use the notification to update your user interface. -Apple Inc.

The state of app must be in UIApplicationStateActive or UIApplicationStateBackground .

You can not do anything if state of application is UIApplicationStateInactive .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM