问题描述
我正在尝试通过推送通知在特定屏幕上打开应用。我的理解是,我使用“深层链接”来执行此操作。我已经设置了react-navigation来处理深层链接(以及AndroidManifest.xml和appDelegate.m),并且可以使用xcrun simctl openurl booted myapp://a/1
/ adb shell am start -W -a android.intent.action.VIEW -d "myapp://a/1" app.bundle.name
打开深层链接。
但是,当应用程序关闭时,我无法通过深度链接获得推送通知以打开该应用程序;只会在主屏幕上(在iOS和Android上)打开应用。如何在深层链接中打开该应用程序(或者读取阅读推送通知的内容,以便自己可以导航至正确的屏幕)?
库版本:
- 本机0.62.2
- @ react-navigation 5.5.1
- react-native-fcm 16.2.4
解决方法
经过一些调试后,我发现这实际上是我的代码处理通知中的问题。我正在检查通知正文中的ID
属性,但并非所有通知都有一个。因此,一旦我删除了它,它便开始工作。因此,要使其正常工作,您应该具有以下内容:
// push handler component
class PushHandler extends React.Component {
componentDidMount() {
FCM.getInitialNotification(notification => {
if (!notification) {
// the app was not opened with a notification.
return;
}
// extract route from `notification` here
this.props.navigation.navigate(route);
}
}
// ... rest of push notification handling code
}
它与深层链接也没有任何关系。 this.props.navigation.navigate
只是普通的应用内导航。