iOS: 如何像Parse一样在用户强行关闭应用时接收推送通知?

iOS: how to receive Push Notifications when user force closes the App like Parse does?

我有一个尚未发布的正在开发中的应用程序,除了我的后端之外,我还依赖于 Parse 进行推送,但是由于 Parse 正在关闭,因此使用 Parse 发布它没有意义以后再改。

无论如何,现在我为了 iOS 转移到 GCM,从我在下面看到的代码中,您只需注册推送通知(正常的 iOS 方式),然后一旦您获得令牌,您就可以注册它使用 GCM(就像以前使用 Parse 一样),但最大的区别是,当我强制关闭应用程序时,永远不会收到推送通知,但是当我使用 Parse 并强制关闭应用程序时,我会发送推送通知我还是明白了,看看 Parse 的开源 SDK 下有什么(我无法理解所有内容,但我发现没有使用 PushKit),Pushkit 在网络上的几个类似问题中被建议使用。但是对于如何实现这一点还没有明确的解决方案。

这是我注册推送的操作:

// Register for remote notifications
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

然后:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Application registered for remote notification");

    GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
    instanceIDConfig.delegate = self;

    [[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
    _registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
                             kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
    [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID
                                                        scope:kGGLInstanceIDScopeGCM
                                                      options:_registrationOptions
                                                      handler:_registrationHandler];

    NSLog(@"This is device token%@", deviceToken);
}

这是我要发送的推送通知:

curl --header 'Content-Type:application/json' --header 'Authorization: key=My-Google-Key' \
       https://android.googleapis.com/gcm/send \
-d '{"to": "/topics/test","notification":{"sound": "default","badge": "1","title": "test title","body": "Test body", "dl": "myscheme://section/e5c7f33080940a95b94b50941e667da6", "content_available":true}, "data":{"alert":"breaking title","title":"breaking title","dl": "myscheme://section/e5c7f33080940a95b94b50941e667da6"", "content_available":true}}'

总而言之,我需要一种方法来在应用程序完全不 运行 的情况下仍然接收推送通知

我找到了解决方案我会把它留在这里以防其他人遇到同样的问题:

都是关于有效负载中的 "priority" 密钥

"priority":10表示优先级:,

curl --header 'Content-Type:application/json' --header 'Authorization: key=My-Google-Key' \
       https://android.googleapis.com/gcm/send \
-d '{"to": "/topics/test","priority":10,"notification":{"sound": "default","badge": "1","title": "test title","body": "Test body", "dl": "myscheme://section/e5c7f33080940a95b94b50941e667da6"}, "data":{"alert":"breaking title","title":"breaking title","dl": "myscheme://section/e5c7f33080940a95b94b50941e667da6"}}'

参考:https://developers.google.com/cloud-messaging/http-server-ref#priority

旁注:您不必复制 "notification" 和 "data" 这只是一个示例,它不是我在我的应用程序中使用的真实负载。