evollu / react-native-fcm

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

Multiple notifcation getting on both ios and androdi #853

Open shimil2017 opened 6 years ago

shimil2017 commented 6 years ago

HI I am getting multiple notifaction .please help me as soon as possible

import {Platform} from 'react-native' import React, { Component } from 'react' import { connect } from 'react-redux' import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm' import {pushtokenSending,loginUpdate,latlong} from '../Login/LoginAction' import {notifcationPressed,notifcationSeened} from './Notification/NotifiAction' class FCMService extends Component { constructor(props) { super(props) }

componentDidMount() { //const {id,token}=this.props; this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {

  if(notif.opened_from_tray){
   if(Platform.OS==='android'){
       console.log(notif,this.state.appState);
     return; 
   }
  }
 if(notif.local_notification){
  return;
    }

    if(Platform.OS==='ios'){
      if(notif.aps.alert){
    //  alert("loopp")
      this.props.notifcationPressed(notif)
      return;
    }
    }else{
        if(notif.fcm){
        this.props.notifcationPressed(notif)
      }
    }
     //this.showLocalNotification(notif);
     FCM.cancelAllLocalNotifications()

})

  this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
    // update token in server
    if(token&&this.props.id&&this.props.token){
    this.props.pushtokenSending({fcmtoken:token,id:this.props.id,token:this.props.token,deviceplatform:Platform.OS==='ios'?'ios':'android'})
  }

});

FCM.getInitialNotification().then(notif=>{
console.log(notif);
});

this.requestPermissions()

this.initToken()

}

componentWillUnmount() { FCM.removeAllDeliveredNotifications() this.notificationListener.remove(); this.refreshTokenListener.remove(); console.log("component un-mounted") }

requestPermissions = () => { if(Platform.OS==='android'){ FCM.requestPermissions(); }else { FCM.requestPermissions({badge: false, sound: true, alert: true}).then(()=>console.log('granted')).catch(()=>console.tron.log('notification permission rejected')) } }

initToken = async () => { const {id,token}=this.props; const latestFcmToken = await FCM.getFCMToken().then(token => { return token }); if(latestFcmToken&&this.props.id&&this.props.token){ //alert(latestFcmToken) this.props.pushtokenSending({fcmtoken:latestFcmToken,id:this.props.id,token:this.props.token,deviceplatform:Platform.OS==='ios'?'ios':'android'}) } // logic for saving sending token to server }

render(){ return null } }

const mapStateToProps=({NotifiReducer,Babies,Login})=>{ const {babyId}=Babies const {id,token,auth}=Login const {notidata,noticount}=NotifiReducer return{ babyId, id, token, notidata, noticount,

} } export default connect(mapStateToProps, {pushtokenSending,notifcationPressed,notifcationSeened})(FCMService)

this is my FCMService component

evollu commented 6 years ago

code looks right to me. What do you mean "getting multiple notification"? callback getting called multiple times? banner showing up multiple time?

shimil2017 commented 6 years ago

Callback getting multiple times.please response as soon as possible.Some time notification coming two times.after console i got that listener calling multiple times.please latest stable version also.should I want to change all linked library when i updating library

evollu commented 6 years ago

no you don't need to change all linked library.

add breakpoint on this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => { make sure componentDidMount doesn't get called multiple times.

Callback getting multiple times could happen for iOS with but _notificationType would be different. for android there shouldn't be multiple callbacks

Torippana commented 5 years ago

why this is happen on ios? any solutions?

NeelamSoni129 commented 5 years ago

@Torippana any solution you got?

Torippana commented 5 years ago

yeah, in my case, it was not react-native-fcm probrem. Rendering multiple times because of the other component, thats why.