我正在使用 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
}
);
现在我有两个问题:
- 我认为这是在我第一次打开应用程序时没有点击任何通知的第一个函数getInitialNotification它将我导航到通知屏幕两三秒钟,然后让我回到家中,然后
- 当我点击我收到的通知“当应用程序关闭时'它不在后台'杀戮'”!,只是停留在主屏幕上没有将我导航到通知屏幕 或导航我我想2秒钟并让我回到主屏幕,
但是当应用程序仍在后台时,下功能“ onNotificationOpened ”工作得很好