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);
});
};
}