我正在使用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
。