As a beginner in the world of react native, I am exploring how to incorporate background notifications into my app. After conducting some research, it seems like using Firebase Cloud Messaging would be the most suitable approach for this.
After going through various tutorials, I have implemented the following code snippet:
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);
}
};
getFcmToken = async () => {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log(fcmToken);
console.log("Your Firebase Token is:", fcmToken);
} else {
console.log("Failed", "No Token Recived");
}
};
async componentDidMount() {
await this.requestUserPermission();
// Register background handler
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Messaage handled in the background!', remoteMessage);
});
};
}
However, upon testing the app on my iOS device, I encountered an error message in the terminal stating:
ReferenceError: Can't find variable: getFcmToken
Furthermore, when attempting to send a test notification, it does not appear as expected.
My query is: Could there be an issue in my code implementation or have I overlooked something?