evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

How can I write action listners when i tap on notification in foreground? #1103

Closed saikiranoptisol closed 5 years ago

saikiranoptisol commented 5 years ago

Hi,

if app is in foreground i'm using FCM.on(FCMEvent.Notification, async (notif) => { to get notifications for background and foreground. If it is from background when i click action method works fine. but if the app is in foreground and receive a notification then it will directly do its action manually without taping on the nottification. My exact function is something like this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => { console.log("INITIAL NOTIFICATION FINAL", JSON.stringify(notif));

        if (Platform.OS === 'ios') {
            if (notif === undefined) {
                this._navigateToMainView()
            } else {
                if (notif.type === '1') {
                    this.props.navigation.navigate('chatList')
                } else if (notif.type === '2') {
                    this.props.navigation.navigate('chatList')
                } else if (notif.type === '3') {
                    this.props.navigation.navigate('chatList')
                    console.debug('data' + JSON.stringify(notif))
                } else if (notif.type === '4') {
                    this.props.navigation.navigate('contactRequest')
                } else if (notif.type === '5') {
                    console.log('notifi 5')
                    let arr = JSON.parse(notif.finalArray)
                    console.log(arr.roomId)
                    if (arr.videoCallRequest) {
                        this.props.callStatus(arr.roomId)
                        this.props.navigation.navigate('VideoCallAcceptOrReject', { senderName: arr.senderFullname, roomId: arr.roomId })
                    }
                    if (arr.isAccept === 'true') {
                        console.log('accept true')
                        this.props.callStatus(arr.roomId)

                        this.props.navigation.navigate('videoCall', { roomId: arr.roomId })
                    } else if (arr.isAccept === 'false') {
                        console.log('accept false')
                       this.props.navigation.navigate('yourMatches', { roomId: arr.roomId })
                    }
                    console.log('video' + JSON.stringify(notif))
                }
            }
        } else if (Platform.OS !== 'ios') {
            if (notif.opened_from_tray === 1) {
                this._navigateToMainView()
            }
        } 
RZzero commented 5 years ago

Hi @saikiranoptisol, where you able to solve this?

saikiranoptisol commented 5 years ago

@RZzero when you tap on notification you will get open_from_tray: true. based on that you can navigate