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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…