我正在使用react-native-firebase在我的应用程序中设置前台和后台推送通知。我根据文档设置了它:https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications#App-in-Foreground-and-background
该应用程序附加了一个firebase.messaging().onMessage()
监听器,当它在前台收到通知时,我使用firebase.notifications.displayNotification(notification)
来显示通知。
然后onNotificationOpened
监听器接收通知并将用户导航到不同的屏幕。
问题是,从onNotificationOpened
收到的通知对象有一个动作android.intent.action.VIEW
,它会在每次单击时启动MainActivity。我不知道这个操作字符串来自哪里,因为客户端应用程序和后端都没有设置任何点击操作。此android.intent.action.VIEW
碰巧是我的MainActivity
的意图过滤器动作。
我不希望ContentIntent操作与我的通知绑定,我希望我的应用程序处理导航到正确的组件。
此外,还有一个问题是,单击通知会卸载并重新安装我的主要组件,这会导致通知侦听器被删除。
我尝试在AndroidManifest中设置MainActivity android:launchMode=singleTop
,但活动仍然重新启动。我也尝试过使用singleTask
,singleInstance
和standard
这是我的显示通知功能,称为onMessage
export async function displayNotification(message: RemoteMessage) {
try {
const notification = new firebase.notifications.Notification();
const { title, message: messageBody, ...messageData } = message.data;
notification
.setNotificationId(message.messageId)
.setTitle(title)
.setBody(messageBody)
.setData({
...messageData
})
.setSound('default');
if (isAndroid) {
// Build a channel for notifications on Android
const channel = await createAndroidChannel();
notification.android
.setBigText(messageBody)
.android.setChannelId(channel.channelId)
.android.setAutoCancel(true) // Remove on press
}
await notifications.displayNotification(notification);
return notification;
} catch (error) {
}
}
预期结果是:用户在前台有app>用户收到推送通知>点击通知>不重新启动主要活动,但如果需要通过onNotificationOpened
监听器重定向到特定屏幕
实际结果是:用户单击前台通知后>重新启动主要活动并重新安装主屏幕,从而导致通知侦听器被删除。
这里的问题是,当您的应用处于前景或墨水背景时,您的应用已经有一个实例正在运行。因此,当您按下通知时,它会再次创建mainActivity的另一个实例并重新启动它。要解决此问题,您必须了解android的启动模式
Please refer to this link for description
尝试在AndroidManifest.xml文件中将launchMode设置为singleTop。然后重新安装并测试应用程序。这应该可以解决你的问题。