0

I am new to react native and have created an app which displays events. I would like to have the app send background notifications each time a new event has been added to the app.

After doing some research I have found that the best way to send background notifications is to use firebase cloud messaging. This seems to only allow messages to be sent from the firebase website console. I was wondering if there is a way to have a notification send when something is added to the app without have to keep going into the firebase console.

I have been able to get firebase to send background notifications on iOS. Here is my code

const getFcmToken = async () => {
  const fcmToken = await messaging().getToken();
  if (fcmToken) {
      console.log("Your Firebase Token is:", fcmToken);
  } else {
      console.log("Failed", "No Token Recived");
  }
};

export default class App extends React.Component {
  requestUserPermission = async () => {
    const authStatus = await messaging().requestPermission();
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL;
      if (enabled) {
        getFcmToken();
        console.log('Authorization status:', authStatus);
      }
    };

    async componentDidMount() {
      await this.requestUserPermission();

       // Register background handler
       messaging().setBackgroundMessageHandler(async (remoteMessage) => {
       console.log('Messaage handled in the background!', remoteMessage);
  });
};
}
CodeLover
  • 166
  • 1
  • 11
  • 34
  • There are two ways to send messages with FCM: 1) through the Firebase console, 2) by calling the FCM API. Calling the API requires that you specify the so-called FCM server key, which (as its name implies) should only be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions. See https://stackoverflow.com/q/37435750, https://stackoverflow.com/a/42838376 and more from https://stackoverflow.com/search?q=%5Bfirebase-cloud-messaging%5D+send+message+device+to+device – Frank van Puffelen Nov 11 '20 at 15:39
  • Thank you for your help – CodeLover Nov 11 '20 at 16:11

0 Answers0