GCM 后台通知适用于 Android;不在 iOS
GCM background notification works on Android; not on iOS
我尝试了以下各种组合:
- aps 负载中传递了空警报
- aps 负载中传递了一个空声音
- 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);
一打开app就有通知,后台没有。
看起来像这样:
[AnyHashable("body"): NTCIP Example Unit: now showing WALNUT TREE
(Tue Nov 15 14:01:30 EST 2016), AnyHashable("id"): idhere,
AnyHashable("collapse_key"): do_not_collapse, AnyHashable("from"):
/topics/global, AnyHashable("title"): Notification of message change]
我的 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) 被调用。
请帮忙!
原来我需要jGcmData.add("notification", jData);而不是 jGcmData.add("data", jData); APNS 处理后台通知。进行更改后,我现在看到正在调用 application(didReceiveRemoteNotification:::fetchCompletionHandler),除了更改我处理有效负载的方式外,我相信我的后台通知问题已解决!
(Link 到帮助我找到此解决方案的 SO 问题将很快添加!)
澄清一下:
GCM 接受负载描述作为通知或数据
APNS 希望它是后台通知的通知
我尝试了以下各种组合:
- aps 负载中传递了空警报
- aps 负载中传递了一个空声音
- 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);
一打开app就有通知,后台没有。
看起来像这样:
[AnyHashable("body"): NTCIP Example Unit: now showing WALNUT TREE (Tue Nov 15 14:01:30 EST 2016), AnyHashable("id"): idhere, AnyHashable("collapse_key"): do_not_collapse, AnyHashable("from"): /topics/global, AnyHashable("title"): Notification of message change]
我的 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) 被调用。
请帮忙!
原来我需要jGcmData.add("notification", jData);而不是 jGcmData.add("data", jData); APNS 处理后台通知。进行更改后,我现在看到正在调用 application(didReceiveRemoteNotification:::fetchCompletionHandler),除了更改我处理有效负载的方式外,我相信我的后台通知问题已解决!
(Link 到帮助我找到此解决方案的 SO 问题将很快添加!)
澄清一下: GCM 接受负载描述作为通知或数据 APNS 希望它是后台通知的通知