我尝试了以下各种组合:
-
在aps有效负载中传递空警报
-
在aps有效负载中传递空声
-
content_available:true
-
content-available:1
优先级设置为高 . 我在目标功能中以后台模式启用了后台提取和远程通知 .
这就是我的有效载荷的样子:
JsonObject jGcmData = new JsonObject();
jGcmData.addProperty("to", "/topics/global");
jGcmData.addProperty("priority", "high");
JsonObject jData = new JsonObject();
jData.addProperty("id", nObj.id);
jData.addProperty("title", nObj.title);
jData.addProperty("body", nObj.body);
JsonObject apsData = new JsonObject();
apsData.addProperty("content-available", "1");
apsData.addProperty("alert", "");
jGcmData.add("aps", apsData);
jGcmData.add("data", jData);
一旦我打开应用程序而不是在后台,通知就会出现 .
它看起来像这样:
[AnyHashable(“body”):NTCIP示例单位:现在显示WALNUT TREE(2016年11月15日星期二14:01:30),AnyHashable(“id”):idhere,AnyHashable(“collapse_key”):do_not_collapse,AnyHashable( “from”):/ topics / global,AnyHashable(“title”):消息更改通知]
收到时,我的AppDelegate用于前台和后台通知:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler handler: @escaping (UIBackgroundFetchResult) -> Void) {
print("background notification received: \(userInfo)")
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
NotificationCenter.default.post(name: NSNotification.Name(rawValue: messageKey), object: nil, userInfo: userInfo)
handler(UIBackgroundFetchResult.noData);
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("foreground notification received: \(userInfo)")
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
NotificationCenter.default.post(name: NSNotification.Name(rawValue: messageKey), object: nil, userInfo: userInfo)
}
我从来没有看到应用程序(didReceiveRemoteNotification ::: fetchCompletionHandler)被调用 .
请帮忙!
1 回答
结果我需要jGcmData.add(“notification”,jData);而不是jGcmData.add(“data”,jData);供APNS处理后台通知 . 进行更改我现在看到应用程序(didReceiveRemoteNotification ::: fetchCompletionHandler)被调用,除了改变我处理有效负载的方式,我相信我的后台通知问题已经解决!
(链接到帮助我找到此决议的SO问题将很快添加!)
为了澄清:GCM接受有效负载描述作为通知或数据APNS期望它是后台通知的通知