当iOS React Native应用在后台收到推送通知时,不会调用onNotification

时间:2019-10-05 16:49:42

标签: ios react-native push-notification background react-native-push-notification

环境

我正在使用react-native-push-notification,它的配置是:

// top level code in index.js
PushNotification.configure({
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },
  onNotification: onNotif,
  senderID: '11223344556677',
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },
  popInitialNotification: false,
  requestPermissions: true,
});

yarn list的相关部分:

├─ react-native@0.60.5
├─ @react-native-community/push-notification-ios@1.0.2
├─ react-native-push-notification@3.1.9
│  └─ @react-native-community/push-notification-ios@^1.0.1

APNS证书已正确设置。 功能页面的背景模式部分启用了背景获取远程通知模式。

此外,如其他答案所述,didReceiveRemoteNotification中有AppDelegate.m方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

发送到APNS的消息是:

{"aps":{"alert": "Test" ,"sound":"default","content-available": 1}}

还尝试了静默有效负载{"aps":{"content-available": 1}}和普通有效负载{"aps":{"alert": "Test" ,"sound":"default"}}

问题

在真实设备(运行iOS 13的iPhone 7)上进行测试时,当应用程序处于前台状态时,它将正确接收到通知(未显示实际的通知,但会调用onNotif)。但是,当应用程序在后台运行(而不是终止运行)时,将不再调用onNotif。我测试了嘈杂的(正常的)和静默的(有content-available且没有消息)的有效负载,结果是相同的。嘈杂的推送通知将显示在通知中心,但在两种情况下都不会调用onNotif

0 个答案:

没有答案