当应用程序处于后台时,未在 iOS 13.3 中调用 didReceiveRemoteNotification

Mar*_*985 19 objective-c apple-push-notifications ios silent-notification ios13

我在敲我的头。我正在实施推送通知。一切正常(收到推送,更新徽章),但在 iOS 13.3 下,应用程序在后台时不会调用方法 application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 。如果应用程序在前台或使用 iOS 12 设备,则调用该方法。我通过以下方式注册推送通知:

[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        });
    }
}];
Run Code Online (Sandbox Code Playgroud)

有效载荷设置为以下

{"aps": {
    "badge": 10,
    "alert": "test",
    "content-available": 1
}}
Run Code Online (Sandbox Code Playgroud)

我尝试在所有变体中添加“远程通知”和“后台处理”作为应用程序功能(仅“远程通知”/“后台处理”,没有任何这些功能,启用两者)而没有任何更改。我为 UNUserNotificationCenter 设置了委托,但同样没有成功。我相应地设置了标题:

curl -v \
 -H 'apns-priority: 4' \
 -H 'apns-topic: xx.xxxxx.xxxx' \
 -H 'apns-push-type: alert' \
 -H 'Content-Type: application/json; charset=utf-8' \
 -d '{"aps": {"badge": 10,"alert": "test", "content-available":1}}' \
 --http2 \
 --cert pushcert.pem \
 https://api.sandbox.push.apple.com/3/device/1234567890
Run Code Online (Sandbox Code Playgroud)

从文档中它指出,即使应用程序在后台,也会调用此方法:

使用此方法为您的应用程序处理传入的远程通知。与 application:didReceiveRemoteNotification: 方法仅在您的应用程序在前台运行时调用的方法不同,当您的应用程序在前台或后台运行时,系统会调用此方法。

对于 iOS 13,我在这里缺少什么?

Zha*_*han 13

你设置了吗

"content-available": 1
Run Code Online (Sandbox Code Playgroud)

在您的后端 APS 负载中?

还需要确保您在 iOS 应用程序的 info.plist 文件中启用了后台模式

<key>UIBackgroundModes</key>
<array>
    <string>processing</string>
    <string>remote-notification</string>
</array>
Run Code Online (Sandbox Code Playgroud)

  • 是的,我做到了。您可以从我最初的帖子中的 CURL 请求中看到。我还添加了 "content-available": 1 并尝试了所有后台模式(启用、禁用、变体),但 application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 仍然没有被调用。仅当应用程序处于活动状态时。 (2认同)

小智 5

此委托方法:-

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
Run Code Online (Sandbox Code Playgroud)

当我们点击 iOS 13 的通知并且应用程序在后台时被调用。

  • 是的,但我正在寻找一种无需单击通知即可获取有效负载的方法。 (2认同)