首页 > 解决方案 > 打开通知后导航到屏幕?

问题描述

我正在使用 Firebase 云功能向特定设备发送远程通知我得到了他的 FCM 令牌,我收到了它并且工作得很好,

这是我通过云函数发送通知的代码

const functions = require("firebase-functions");
const admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://-----.firebaseio.com"
});
exports.sendPushR = functions.database
  .ref("/request/{pid}/orders/{orderid}")
  .onCreate(async (snapshot, context) => {
    const registrationTokens = snapshot.val().token;
    const event = context.params;
    const afterData = snapshot.val();
    const username = snapshot.val().username;

    const payload = {
      notification: {
        title: "New Order",
        body: `You received a new order from ${username} check it now! `,
        sound: "default",
        icon: "default"
      }
    };

    try {
      const response = await admin
        .messaging()
        .sendToDevice(registrationTokens, payload);
      console.log("Successfully sent message:", response);
    } catch (error) {
      console.log("Error sending message:", error);
    }
    return null;
  });

并在主屏幕应用程序中

  async componentDidMount() {
//BackHandler.addEventListener("hardwareBackPress", this.backPressed);

this.notificationInitListener = await firebase
  .notifications()
  .getInitialNotification()
  .then(notificationOpen => {
    if (notificationOpen) {
      console.log(notificationOpen);
      setTimeout(() => {
        this.props.navigation.navigate("Notifications");
      }, 5000);
      console.log("1---OPEND");
      firebase.notifications().removeAllDeliveredNotifications();
      console.log("1---OPEND-After");
    }
  });
this.removeNotificationOpenedListener = firebase
  .notifications()
  .onNotificationOpened(notificationOpen => {
    // Get the action triggered by the notification being opened
    // const action = notificationOpen.action;
    // Get information about the notification that was opened
    // const notification = notificationOpen.notification;
    if (notificationOpen) {
      this.props.navigation.navigate("Notifications");
      console.log("OPEND");
      firebase.notifications().removeAllDeliveredNotifications();
      console.log("OPEND-After");
    }
  });
 }

路线

const HomeStack = createStackNavigator(
  {
    Home: {
      screen: Home,
      navigationOptions: ({ navigation }) => ({
        title: "Home",
        headerLeft: <NavigationDrawerStructure navigationProps={navigation} />,
        headerRight: (
          <TouchableOpacity
            onPress={() => navigation.navigate("Notifications")}
            style={{ margin: 10 }}
          >
            <Icon name="ios-notifications" size={28} color="#1DA1F2" />
          </TouchableOpacity>
        )
      })
    },
    MapScreen: {
      screen: MapScreen,
      navigationOptions: {
        title: "Map"
      }
    },
    ProviderProfile: {
      screen: ProviderProfile
    },
    GalleryDetails: {
      screen: GalleryDetails,
      navigationOptions: {
        title: "Gallery Details"
      }
    },
    Order: {
      screen: Order,
      navigationOptions: {
        title: "Order"
      }
    },

    Favorites: {
      screen: UserFavorites,
      navigationOptions: ({ navigation }) => ({
        title: "My Favorites!",
        headerLeft: <NavigationDrawerStructure navigationProps={navigation} />
      })
    },
    Notifications: {
      screen: Notifications,
      navigationOptions: {
        title: "Notifications"
      }
    }
  },
  {
    defaultNavigationOptions
  }
);

现在我有两个问题:

  1. 我认为这是在第一个函数getInitialNotification中,这是我第一次打开应用程序时没有单击任何通知,它会将我导航到通知屏幕两三秒钟,然后让我回到家中,并且
  2. 当我点击我收到的通知“当应用程序关闭时'它不在后台'杀死'”!,只是停留在主屏幕不导航我到通知屏幕 或导航我我想 2 秒然后让我回到主屏幕,

但是当应用程序仍在后台时,下面的函数“ onNotificationOpened ”工作得很好

演示 https://vimeo.com/350006721

标签: javascriptandroidreactjsfirebasereact-native

解决方案


你没有解释你的期望..但我认为你不想从那个通知屏幕开始,而是从主屏幕开始。

为此,您可以initialRouteName为您的方法使用 as 第二个参数createStackNavigator(参见本页最底部的示例:https ://reactnavigation.org/docs/en/stack-navigator.html )

试试看,如果解决了,去寻找第二个问题(我更喜欢一步一步解决问题)


推荐阅读