Reputation: 955
I send from my webspace via PHP push notifications to my swift 2 app. I do it like this was:
$body['aps'] = array('alert' => 'HELLO', 'badge' => 1, 'sound' => 'default');
Now I would like to working with silent push notifications, too. I learned, that I can send silent push like this:
$body['aps'] = array('content-available' => 1);
And in my app delegate I do something after receiving a silent push.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("DO SOMETHING IN THE BACKGROUND")
handler(UIBackgroundFetchResult.NewData)
}
return
}
This works fine. But the problem now, is that this print will be come each time, if I send a normal push, too.
That is not correct. The print "DO SOMETHING IN THE BACKGROUND" should only execute if a silent push will be receive.
What am I doing wrong?
Upvotes: 0
Views: 1569
Reputation: 672
Modify your code in the following way:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
if let apsValue = userInfo["aps"] as? NSDictionary {
if (apsValue["content-available"] as? Int == 1){
print("DO SOMETHING IN THE BACKGROUND")
handler(UIBackgroundFetchResult.NewData)
}
}
return
}
Right now your action item is being executed every single time but the above code will filter it out so that it only executes when there is a silent push (which as you know is indicated by the content available = 1 flag.)
Upvotes: 1