I know that it has a lot of questions about this theme, but no one works in my project, and I don't know why.
I'm using RNF on my project to handle push notification. Everything is set up correctly, but to get notification working, I have to first kill the app, relaunch, and only then I receive notification.
This issue happen in both ios and android.
Here is a sample of my code (bootstrapNotification.js)
async function requestUserPermission(): Promise<any> {
if (!messaging().isDeviceRegisteredForRemoteMessages) {
await messaging().registerDeviceForRemoteMessages();
}
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
return enabled;
}
async function getToken(): Promise<any> {
return messaging()
.getToken()
.then((token) => {
return token;
});
}
export default async function startNotification() {
const enabled = await requestUserPermission();
const token = await getToken();
NotificationApi.subscribe(token).subscribe();
if (token && enabled) {
log.info('onMessage started');
messaging().onMessage(async (remoteMessage) => {
log.info('A new FCM message arrived!', remoteMessage);
});
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
log.info('Message handled in the background!', remoteMessage);
});
messaging().onNotificationOpenedApp((remoteMessage) => {
log.info(
'Notification caused app to open from background state:',
remoteMessage,
);
});
messaging()
.getInitialNotification()
.then((remoteMessage) => {
if (remoteMessage) {
log.info(
'Notification caused app to open from quit state:',
remoteMessage,
);
}
});
} else {
log.error('Error token or authrorization');
}
}
some information :
"@react-native-firebase/messaging": "^7.4.2",
"@react-native-firebase/app": "^8.2.0",
"react-native": "0.63.3",