Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

ios - Update local database in the background through silent push notification when app is not running

Is it possible to receive a silent push notification ("content-available": "1") and run a function to change data in NSUserDefaults.standardUserDefaults() without the user having to tap the notification (there is no notification in the notification center, since it's a silent push notification). The app is not running at all (e.g. force-quit by user). I have tried the code below and it worked if the app was running or in the background, but I also want it to work if the app was force-quit (not running at all).

I have enabled background fetch and remote notifications.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    handleNotification()
}

//this works, the someData is not a problem
func handleNotification() -> Void{
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setValue(someData, forKey: "key")
    defaults.synchronize()
}

When I send push notifications, if the app is active or in background, the data is updated, but if the app is not running at all, the data is not updated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer: There is (almost) no way to do anything without user interaction, when the app was force-quit (is not running).

For explanation see the documentation:

  • Handling Remote Notifications - application:didReceiveRemoteNotification:fetchCompletionHandler:

    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. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

  • Understanding When Your App Gets Launched into the Background:

    Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:

    • For location apps: The system receives a location update that meets the app’s configured criteria for delivery. The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
    • For audio apps, the audio framework needs the app to process some data. (Audio apps include those that play audio or use the microphone.)
    • For Bluetooth apps: An app acting in the central role receives data from a connected peripheral. An app acting in the peripheral role receives commands from a connected central.
    • For background download apps: A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1. The system wakes the app at opportunistic moments to begin downloading new content.
    • For apps downloading content in the background using the NSURLSession class, all tasks associated with that session object either completed successfully or received an error. A download initiated by a Newsstand app finishes. In most cases, the system does not relaunch apps after they are force quit by the user.

    One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.

As you see, only exception are location apps, but this won't help you. Just deal with the fact, that this is an OS limitation and you can't do anything about it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...