1

i have a problem when i want to process notification that is received when the app is in the background. I do receive the notification, when i click on the notification, the app opens however the FCM.on method is not executed at all, it seems it does not handle the callback.

When the app is in the foreground everything is working fine, and it process the notification without any problems.

This is the code in react-native

FCM.on(FCMEvent.Notification, notif => {
      switch(notif.fcm.action){
        case 'fcm.ACTION.HANDLEMESSAGE':
          var data = {
            thread: notif.pk,
            message: JSON.parse(notif.message)
          }

          this.props.dispatch(fcmMSG(data));
          this.props.dispatch(addNotification(notif.pk, notif.job_id));
          break;

        case "fcm.ACTION.HANDLEAPPLICATION":
          axios.get(newServerURL + "/jobs/companyJobDetails/" + notif.recruitJobId + "/")
              .then((response) => {
                  var jobMainRecruits = response.data;
                  const {navigator} = this.refs;
                  navigator.push({
                  jobMainRecruits,
                  });
            })
              .catch((error) => {
                  console.log("ERROR")
            });        
          break;

        default:
      }
    });  
FCM.getInitialNotification().then(notif => {
      switch(notif.fcm.action){
        case 'fcm.ACTION.HANDLEMESSAGE':

          const { navigator } = this.refs;

          var thread = {
            candidate: notif.candidate,
            company: notif.company,
            candidate_avatar: notif.candidate_avatar,
            candidate_first_name: notif.candidate_first_name,
            candidate_last_name: notif.candidate_last_name,

            job_id: notif.job_id,
            pk: notif.pk,
            subject: notif.subject,
            message: JSON.parse(notif.message)
          }

          navigator.push({
            thread,
          });

          break;
        case "fcm.ACTION.HANDLEAPPLICATION":
          axios.get(newServerURL + "/jobs/companyJobDetails/" + notif.recruitJobId + "/")
              .then((response) => {
                  var jobMainMessages = response.data;
                  const {navigator} = this.refs;
                  navigator.push({
                  jobMainMessages,
                  });
            })
              .catch((error) => {
                  console.log("ERROR")
            });          
          break;

        default:

      }
    })

EDIT: this is my code in the backend

push_service = FCMNotification(api_key=FCM_API_KEY)
                    registration_id = token.token
                    message_title = jobInfo.title
                    message_body = "New application"
                    message_icon = 'ic_launcher'
                    sound = 'Default'
                    color = '#362F64'
                    click_action='fcm.ACTION.NEWJOBAPPLICATION'
                    data_message = {'recruitJobId': self.request.data['job'], 'message_title': message_title, 'message_body': message_body}
                    result = push_service.notify_single_device(click_action=click_action, registration_id=registration_id, message_title=message_title, message_body=message_body, message_icon=message_icon, sound=sound, data_message=data_message, color=color)

Thanks for help in advance.

Proxy
  • 1,118
  • 2
  • 10
  • 27

1 Answers1

1

The initial notification contains the notification that launches the app. If the user launches the app by clicking the banner, the banner notification info will be here rather than through FCM.on event. sometimes Android kills activity when the app goes to background, and then resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events. source

FCM.getInitialNotification().then(notif => {
    // do some logic here
   // I usually add this function inside my root page
});
Ray
  • 9,184
  • 3
  • 27
  • 44
  • ah sorry i have it on my getInitialNotificiation as well... Forgot to post that part. Also that code as well is not execute when i resume my app from the background – Proxy Oct 03 '17 at 13:55