push notifications and background fetch mode

2019-08-11 05:27发布

问题:

It's my understanding that the system will either wake you from being closed or running in the background, and allow you to fetch BEFORE it displays the system alert to the user.

I have to download some data to show the user when they tap a push notification.

I am seeing my fetch activities being performed WHILE the push is shown to the user. It is allowing user to tap the notification and launch the app before we've completed the data fetch.

Is this correct?

回答1:

I didn't have the chance to use push notifications and background fetch mode myself,

but according to the docs:

the system sends the notification to your app (launching it if needed)
and gives it a few moments to process the notification before displaying
anything to the user. You can use those few moments 
to download content related to the push notification and
be ready to display it to the user.

so as i understand you are suppose to have some time to react (download data) to a notification before presenting something to the user and not while the notification is shown

the docs don't mention what is considered "few moments" (not that i have seen).

so maybe the download operation is taking more than those "few moments"

hope this helps



回答2:

Using so-called "silent" push notification in iOS7 you can send specific silent push from your server. You need to add additional key content-available in your aps dictionary

"aps" : {
         "alert" : "alert",
         "sound" : "sound",
         "badge" : badge,
         "content-available" : 1
}

Then you need to set two background modes in your project: Remote notifications and Background fetch.

Now every remote push will call for

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

and you can implement any background logic here. For example you can start some fetch process (no longer then 30 seconds!), show custom local notification (which substitutes for standard remote on-screen push) at the moment or on fetch completion.

Be sure that push notifications are allowed for your app in Notification center and that you allow Background App Refresh for your app in Settings/General.

Another important thing is that silent pushes work until you unload you app from background manually (double tap on home button).

Note that this silent mechanism worked incorrectly until iOS7.1



回答3:

You are in control to choose if to show the AlertView even if using third party like UrbanAirShip. Any how, you can decide not to show it, initiate background fetch with completion block. After that you can use local notification to show what ever data you need to show.

Behaviour is little different in iOS 7 and below it.

If you can provide some more code/implementation I might be able to help a bit more.