【发布时间】:2019-09-05 12:50:14
【问题描述】:
我正在使用 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) {
}
}
预期结果是:用户在前台有应用 > 用户收到推送通知 > 点击通知 > 不会重新启动 Main Activity,但如果需要,通过 onNotificationOpened listener 重定向到特定屏幕
实际结果是:用户点击前台通知后 > Main Activity 重新启动,主屏幕重新挂载,导致通知侦听器被删除。
【问题讨论】:
标签: android react-native push-notification foreground react-native-firebase