Karthik CS
Karthik CS

Reputation: 41

how to open a app when its in quit state while receiving fcm push notification - REACT NATIVE

Planning to build an app like uber we have two apps one for end users other for drivers, if end users send a booking notification we need to display in driver app push notification as 'cancel' and 'accept' button with sound like ringtone or we need to open the app automatically, if the app is in quit state or background state. Technologies used: - react native, firebase. Please help me to resolve and am eagerly waiting for your valuable suggestions and ideas

Upvotes: 2

Views: 2367

Answers (3)

Hassan Haroon
Hassan Haroon

Reputation: 34

 useEffect(() => {
    // Assume a message-notification contains a "type" property in the data payload of the screen to open

    messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate('TransactionWithNotification', remoteMessage);
    });

    // Check whether an initial notification is available
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          navigation.navigate('TransactionWithNotification', remoteMessage);
        }
        setLoading(false);
      });

    messaging().onMessage(async remoteMessage => {
      console.log('Notification foregorund state:', remoteMessage);
      navigation.navigate('TransactionWithNotification', remoteMessage);
    });
  }, []);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Upvotes: 0

Karthik CS
Karthik CS

Reputation: 41

https://0x1bitcrack3r.medium.com/incoming-call-notifications-for-react-native-apps-ef4725702401 - using this blog i can able to change the push notification ui and able to trigger ringtone like whats app call

Upvotes: 1

Omkar Tondawalkar
Omkar Tondawalkar

Reputation: 225

i would suggest you to go for https://rnfirebase.io/ using this you can have an node app or use firebase function to push notification to the device which is registered here you would use the following

onNotificationOpenedApp : When the user presses a notification displayed via FCM, this listener will be called if the app has opened from a background state.https://rnfirebase.io/reference/messaging

onNotificationOpenedApp(listener: (message: RemoteMessage) => any): () => void;

Upvotes: -1

Related Questions