我的有效负载是否格式错误,在后台调用didReceiveRemoteNotification?
According to these (and many more) questions on SO:
- stackoverflow.com/questions/18856204
- stackoverflow.com/questions/20741618
- stackoverflow.com/questions/31967093
- stackoverflow.com/questions/19068762
- stackoverflow.com/questions/30936507
I figured out, that the main keys for silent push-notification and for the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
to be called in the background are: the payload, iOS version > iOS 7, enabled background mode with "Remote notifications" and registering push notifications in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.
I did all of those things, but the method mentioned above, that supposed to be called in the background, is only being called while the app is foreground. This is how I form my payload in php:
$contents = json_encode(array(
'aps' => array(
'content-available' => '1',
'sound' => '',
'alert' => '',
),
'where' => array(
'deviceType' => array(
'$in' => array('ios', 'android'),
),
),
'data' => array(
'title' => $this->subject,
'msg' => $this->message,
'date' => date('d.m.Y H:i', time() + (60 * 60 * 2)),
'key' => APPKEY,
'ids' => $ids,
),
));
and this is what I get in Xcode console, as I am able to NSLog the payload when app is in foreground:
{
aps = {
};
date = "21.10.2015 11:01";
ids = (
259,
257,
256,
);
key = xpy3fq4t3wn9;
msg = test11;
title = aatest1;
}
What am I doing wrong? Why isnt "content-available = 1" in Xcode console? Or is there any other reason why appDelegate method isnt being called in background?
Im using iOS 8.1.3, Parse as Push-Notification provider.
I was able to solve my problem just by trying different payload combinations, ended up with this one:
$contents = json_encode(array(
'where' => array(
'deviceType' => array(
'$in' => array('ios', 'android'),
),
),
'data' => array(
'title' => $this->subject,
'msg' => $this->message,
'date' => date('d.m.Y H:i', time() + (60 * 60 * 2)),
'key' => APPKEY,
'ids' => $ids,
'aps' => array(
'content-available' => '1',
'sound' => '',
'alert' => '',
),
),
));
Looks like 'aps' has to be in 'data' array. Now the app delegate method is being called in background and I can see "content-available = 1" in Xcode console.