gpt4 book ai didi

javascript - 打开通知后导航到屏幕?

转载 作者:行者123 更新时间:2023-12-03 07:08:31 28 4
gpt4 key购买 nike

我正在使用 Firebase 云功能向特定设备发送远程通知我得到了他的 FCM token ,我收到了它并且运行良好,

这是我通过云功能发送通知的代码

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. 当我点击我收到的通知“当应用程序正在关闭时‘它不在后台’Killing’!”,只是停留在主屏幕,无法导航到通知屏幕或者导航我,我想了 2 秒钟,然后让我回到主屏幕,

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

演示 https://vimeo.com/350006721

最佳答案

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

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

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

关于javascript - 打开通知后导航到屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57171787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com