gpt4 book ai didi

javascript - react-native-firebase getInitialNotification 循环

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:24:22 29 4
gpt4 key购买 nike

我使用 React Native Firebase 在 React Native 中接收通知。当在后台收到消息并单击通知时,将触发 getInitialNotification。如果我导航到另一个屏幕然后回到我的 HomeActivity,getInitialNotification 将再次被触发。如何实现只触发一次?

import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text, YellowBox, Button } from 'react-native';
import StyleIndex from './css/StyleIndex';

import firebase from 'react-native-firebase';
import type { Notification, NotificationOpen } from 'react-native-firebase';


export default class HomeActivity extends Component {
state = { currentUser: null }

componentDidMount() {
const { currentUser } = firebase.auth()
this.setState({ currentUser })

var collectionReference = firebase.firestore().collection('users');
var query = collectionReference.where("phone", "==", currentUser.phoneNumber);
// console.log(firebase.messaging().getToken());
firebase.messaging().hasPermission()
.then(enabled => {
if (enabled) {
firebase.messaging().getToken().then(token => {
query.get().then(function(querySnapshot) {
if (querySnapshot.size == 1) {
firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
fcmtoken: token,
})
} else {console.log('no or more than one documents found')}
});
})
// user has permissions
} else {
firebase.messaging().requestPermission()
.then(() => {
alert("User Now Has Permission");
console.log('now has permission');

})
.catch(error => {
alert("Error", error)
// User has rejected permissions
});
}
});
this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken => {
query.get().then(function(querySnapshot) {
if (querySnapshot.size == 1) {
firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
fcmtoken: fcmToken,


})
.catch(error => {
alert("Error", error)
// User has rejected permissions
});
}
});
this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken => {
query.get().then(function(querySnapshot) {
if (querySnapshot.size == 1) {
firebase.firestore().collection('users').doc(querySnapshot.docs[0].ref.id).update({
fcmtoken: fcmToken,
})
} else {console.log('no or more than one documents found')}
});
});

// DEFAULT CHANNEL
const channel = new firebase.notifications.Android.Channel('default', 'Default Channel', firebase.notifications.Android.Importance.Max);
channel.setDescription('My default channel');
// Create the channel
firebase.notifications().android.createChannel(channel);

// data-only messages from FCM
// https://rnfirebase.io/docs/v4.2.x/messaging/receiving-messages
this.messageListener = firebase.messaging().onMessage((message) => {
// Process your message as required
console.log('onMessage', message);
});

// LOCAL NOTIFICATION: FOREGROUND
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
console.log('onNotificationDisplayed', notification);
});

this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
// Process your notification as required
console.log('onNotification', notification);
notification.android.setChannelId('default');
notification.android.setAutoCancel(true);
firebase.notifications().displayNotification(notification);
});


// APP FOREGROUND / BACKGROUND
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
console.log('onNotificationOpened - action', action);
// Get information about the notification that was opened
const notification: Notification = notificationOpen.notification;
notification.android.setChannelId('default');

// firebase.notifications().displayNotification(notification);
console.log('onNotificationOpened - notification', notification);

firebase.notifications().displayNotification(notification)
});

// APP CLOSED
firebase.notifications().getInitialNotification()
.then((notificationOpen: NotificationOpen) => {
if (notificationOpen) {
// App was opened by a notification
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
console.log('getInitialNotification - action', action);
// Get information about the notification that was opened
const notification: Notification = notificationOpen.notification;
// notification.android.setChannelId('default');
notification.android.setAutoCancel(true);
console.log('getInitialNotification - notification', notification);
// Display the notification
// firebase.notifications().displayNotification(notification);
alert('getInitialNotification');
firebase.notifications().removeAllDeliveredNotifications();
firebase.notifications().cancelAllNotifications();
}
});

}
componentWillUnmount() {
this.onTokenRefreshListener();
this.notificationDisplayedListener();
this.notificationListener();
this.notificationOpenedListener();
}

最佳答案

在循环中调用通知的原因是您正在使用远程通知调用 firebase.notifications().displayNotification(notification);

相反,您应该创建一个新的本地通知,并显示它而不是远程通知。

const localNotification = new firebase.notifications.Notification({
sound: "default",
show_in_foreground: true
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.android.setChannelId("notification_channel_id")
.android.setPriority(firebase.notifications.Android.Priority.High);

firebase.notifications().displayNotification(localNotification);

关于javascript - react-native-firebase getInitialNotification 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728748/

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