I am trying to setup push notification on ios and android using Firebase/mesaging.
I have followed the setup part using the googleservice-info and so on... I have activated push notification and both Background mode capabilities we need. I pushed the APN key on firebase for the ios.
This is my code :
index.js
import { getMessaging } from '@react-native-firebase/messaging';
import firebase from '@react-native-firebase/app';
const credentials = {...}
if (!firebase.apps.length) {
firebase.initializeApp(credentials).then((app) => {
getMessaging(firebase).setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
})
}
Then in my app.tsx
const getFcmToken = async () => {
console.log("trying to get")
try {
const newFcmToken = await messaging().getToken() // HERE
console.log('newFcmToken');
console.log(newFcmToken);
} ...
}
const authStatus = await messaging().requestPermission();
if (!messaging().isDeviceRegisteredForRemoteMessages) {
await messaging().registerDeviceForRemoteMessages();
}
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
await getFcmToken();
}
package-lock.json
"@react-native-firebase/app": "^18.4.0",
"@react-native-firebase/messaging": "^18.4.0",
and then I get a
ERROR [Error: [messaging/unknown] The operation couldn’t be completed. No APNS token specified before fetching FCM Token]
I try to add this in the info.plist
<key>FirebaseAppDelegateProxyEnabled</key>
<true/>
<key>GCMessagingSenderId</key>
<string>...</string>
```
At some point I remembered succeeding having a token, I have one on the firebase dashboard.
I spent 3 days to fixed it and it definitely work fine for me.
You need to add more code to ios/AppDelegate.mm as follows:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[FIRApp configure];
[application registerForRemoteNotifications]; // <--- Add this Line
...
}
You need to add another function to ios/AppDelegate.mm too:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
}
If you are using the iOS Simulator then close it with "Force Quit" and restart the react native application the error might go way in my case I have updated the following:
"@react-native-firebase/app": "^21.2.0" -> "^21.6.2"
"@react-native-firebase/messaging": "^21.6.0",-> "^21.6.2"
And Added the Analytics as well:
"@react-native-firebase/analytics": "^21.6.2",
Then at some point I got the errors:
Error: [messaging/unknown] The operation couldn’t be completed. No APNS token specified before fetching FCM Token
Restarted the simulator and tried again I obtained the Permission and the Token as well and got the pushNotification to work as it was.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With