【问题标题】:How do I remove react native firebase notification click action on android?如何删除 android 上的 react native firebase 通知点击操作?
【发布时间】: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,但该活动仍会重新启动。我也尝试过使用singleTasksingleInstancestandard

这是我的显示通知功能,称为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


    【解决方案1】:

    这里的问题是,当您的应用处于前台或墨迹背景时,已经有一个应用实例正在运行。所以当你按下一个通知时,它会再次创建mainActivity的另一个实例并重新启动它。要解决这个问题,你必须了解android的启动模式

    Please refer to this link for description

    尝试在 AndroidManifest.xml 文件中将 launchMode 设置为 singleTop。然后重新安装并测试应用程序。这应该可以解决您的问题。!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2020-07-05
      • 1970-01-01
      相关资源
      最近更新 更多