我已经将 firebase 云消息传递集成到应用程序中,我使用了他们的库并遵循了官方文档:https://rnfirebase.io/messaging/usage,我从上到下阅读了所有内容并做了所有必要的配置。
我的问题是无论我尝试什么,
messaging().onNotificationOpenedApp(callback)
和 messaging().getInitialNotification
都不运行。 onMessage
和 setBackgroundMessageHandler
工作完美期待前两个。我最初的问题是 setBackgroundMessageHandler
也不会运行,但是能够通过使用 apns
键直接从我的后端发送通知来解决这个问题,如下所示:
const message = {
notification: {
title: 'test',
body: 'body',
},
data: {
routeRedirect: 'Page',
},
apns: {
payload: {
aps: {
sound: 'default',
contentAvailable: true,
},
},
headers: {
'apns-priority': '5',
},
},
token:
'my fcm token is here',
};
try {
const res = await firebaseAdmin.messaging().send(message);
console.log(res);
} catch (error) {
console.log('error', error.message);
}
现在它适用于那部分。我在 iOS 的通知面板上收到通知,并且在我的终端中看到通知到达时正在调用
setBackgroundMessageHandler
,这是预期的。
这是手机代码:
// requests permissions
firebaseNotifications.requestUserPermission();
// prints my token to the console
firebaseNotifications.getFCMToken();
messaging().onMessage((remoteMessage: FirebaseNotification) => {
console.log('New FCM Message -> ', JSON.stringify(remoteMessage));
});
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('onNotificationOpenedApp', remoteMessage.data);
});
messaging().setBackgroundMessageHandler(remoteMessage => {
console.log('setBackgroundMessageHandler', remoteMessage);
return '';
});
messaging()
.getInitialNotification()
.then(remoteMessage => {
console.log('getInitialNotification', remoteMessage);
});
function HeadlessCheck({ isHeadless }) {
console.log(isHeadless, ' is headless?');
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
AppRegistry.registerComponent(appName, () => HeadlessCheck);
当我将应用程序置于后台然后点击通知时,应用程序打开但
onNotificationOpenedApp
不运行,当我完全关闭应用程序并单击通知时,getInitialNotification
返回空。
有一件事可能与此有关,那就是Headless keeps return true。因此,如果我的应用程序处于退出状态,我会发送一个通知,并且可以看到该应用程序在终端中快速加载,这是预期的。但是当我从通知或其他方式打开应用程序时,isHeadless 是真的,这很奇怪,因为只有当应用程序在后台打开通知时它才应该是真的,但不知何故它仍然是真的。
这是我添加到 AppDelegate.mm 以获取 isHeadless 值的代码(否则每次都会未定义)。
NSDictionary *initProps = [self prepareInitialProps];
// UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"myapp", initProps);
NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];
NSMutableDictionary *initialProps = [initProps mutableCopy];
[initialProps addEntriesFromDictionary:appProperties];
通常只有前两行存在,但我注释了秒并在其下面添加了代码。
我不知道 isHeadless 错误是否与处理程序有关。
package.json 有这个版本的库:
"@react-native-firebase/app": "16.4.6",
"@react-native-firebase/messaging": "16.4.6",
请注意,在 android 上,所有事件处理程序都可以正常工作,只有 ios 有此问题。
我很乐意提供您可能需要的任何信息来解决这个问题。我已经探索了互联网的所有角落以寻找潜在的解决方案,但没有找到。