Notification sound delay when app is in quite state

Viewed 96

Notification is received instantly but notification sound is played in delay(5 or 10 sec) Here is my setbackgroundHandler and notification is sent using node.js and firebase post API, when the app is in background sound is play instantly but in quit/killed state sound is played after 5 or 10 sec(delayed)

AppRegistry.registerComponent(appName, () => App);

// Register  background handler
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
  console.log('Message handled in the background', remoteMessage);
  play();
});

const play = () => {
  var whoosh = new Sound('notfi.mp3', Sound.MAIN_BUNDLE, (error) => {
    if (error) {
      console.log('failed to load the sound', error);
      return;
    }
    // loaded successfully
    console.log(
      'duration in seconds: ' +
        whoosh.getDuration() +
        'number of channels: ' +
        whoosh.getNumberOfChannels()
    );

    // Play the sound with an onEnd callback
    whoosh.play((success) => {
      if (success) {
        console.log('successfully finished playing');
      } else {
        console.log('playback failed due to audio decoding errors');
      }
    });
  });
};

const notification_body = {
  notification: {
    title: notification.title,
    body: notification.description,
  },
  registration_ids: tokens,
};

return axios.post(`${process.env.FIREBASE_FCM_URL}`, notification_body, {
  headers: {
    Authorization: `key=${process.env.FIREBASE_SERVER_KEY}`,
    'Content-Type': 'application/json',
  },
});
0 Answers
Related