问题
I'm building a iPhone application which depends data from an online database.
To update the data in the app i could check at a certain time interval if an update is necessary but it is way cooler if i could use a push services which sends a notification to the app letting it know it is time for an update.
i'm not talking visible push notifications here, just a invisible push notification to fire the update method in my app.
Is there a standard way to do this or could i use apple's push notification services for this purpose ?
With other words: i'm now using pull to get updates, is there a push way to let the backend of my app know it is time for an update?
Edit: And if it is impossible, what would be good time interval for the update (0.03 kb if there are no updates). Is it to much to check it every 30 seconds ?
回答1:
There is a well explained documentation in the Apple Online Library.
With Apple Push Notification Service (APNS) you can get ANY combination of:
- An alert message to display to the user
- A number to badge the application icon with
- A sound to play
When I say any I mean that no alert, no badge and no sound are also possible. Remote notifications are available even if the application is closed (but at least once executed, for register into the notification service), iOS has the debt of manage the push and wake up your application.
If you want to use APNS you need
- a web server (will generate the push)
- a CSR from the web server
- an apple certificate trusting your server (this is the reason of CSR)
- an iOS application with an App ID configured for Notifications
Everything related with CSR and trusting your server is detailed in the iOS provisioning portal, "App ID" section, "How to" tab. Try this link.
In your web server must be hosted the APNS provider who will do these two actions:
Register a token identifying a concrete installation on a concrete iOS device. This token is generated for the Apple APNS and will be sended to the Provider by your app.
Generate push notifications: Will be sended from your provider to Apple APNS, an Apple APNS will delivery to your app (with an alert and/or badge and/or sound and/or silence mode)
The APNS notification will be delivered to your app using the Remote Notification System.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
You can look into the Easy APNS App Delegate
As a provider you can use your own developed or you can use/modify anyone already downloadable like
- Easy APNS
- Java APNS
- Javapns
- etc
So the answer is YES, it is possible. Using Easy APNS esamples, the push generation must look like this:
$apns->newMessage(1);
$apns->addMessageCustom('acme2', array('bang', 'whiz'));
$apns->queueMessage();
回答2:
Yes, it is possible with iOS 7+
You can receive "background" push notifications if you override this method of UIApplicationDelegate: application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
// do what you need i.e. download fresh content (max. 30 seconds)
completionHandler(UIBackgroundFetchResult.NoData)
}
As documentation of this method method says:
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives.
Don't forget to enable "Background fetch" and "Remote notifications" in your background modes.
More info about background execution here.
回答3:
You cannot send an invisible push notification while the application is in background. The only way is to update the data when the application comes to the foreground.
You would check at a certain time interval if an update is necessary or make a pull connection with the server.
回答4:
You can receive notification while you are in foreground, but the server won't make any difference if you are in background or in foreground, unless you send an information to the server that you are in foreground.
That's why in your case, ase described in the previous message, it's better to check with the server when you came from background or when the application starts if there is any upload.
The other option is to send a visible notification so the user will start the application and then the update (as described in the previous paragraph) will happens
回答5:
Use the delegate methods applicationDidRecieveRemoteNotification to tell the app to check in with the update service. Or you can have the app poll the update service durin applicationwillEnterForgound of you don't want to set up push notifications.
回答6:
I dont' think your is the right approach. Consider all the stuff You will build for a simple task: 1) server for push 2) registering for notification 3) going background and resuming (the big and complicated is the server for push notification)
and anyway when You got a message, you must ask the data... no savings comparing asking directly.
So a clean solution can be: 1) ask to the server using normal HTTP (using async NSURL request..) for example: http://...... &lastupdate='2012:05:01 18 00' passing the last update date we got a successful download (You can also use a unicx time stamp...)
2) the server will compare that date with its internal last update date: if it has newer date, it will respond with XML or plist (i prefer plist...)
3) the app downloads new data and updates the date/time.
4) if no data the answer is simply an empty string or for example the same date we sent.
回答7:
Short answer to your first question: You have to poll.
Why :- When your app is in the background, it can not know of any push notifications, unless there is an alert and user clicks View
or Launch
, which is not guaranteed (and you don't want the alert too).
You may also want to avoid using APNS (if its only for this purpose), because of the additional overhead of configuring a server with Apple certificates and all that jazz.
To answer to your second question: How frequent you want your updates really depends on what your app does. For example, if you are showing "Stock" values, you may want to update the data every few seconds. For weather, it may be a few hours. For others, it may be days or even longer. It depends on how critical the updates are for the user.
来源:https://stackoverflow.com/questions/8716470/ios-push-services-is-an-invisible-push-notification-possible