I have followed all the steps that were needed to perform on apple developer account and firebase messaging but still not getting notifications on IOS. I am sending notifications through FCM tests.
It works fine on android. The provisioning profile is added on XCode as well as the required capabilities.
I am using these versions:
"@react-native-firebase/app": "^20.1.0",
"@react-native-firebase/messaging": "^20.5.0",
async function requestUserPermission() {
if (Platform.OS === 'android') {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
}
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
if (Platform.OS === 'ios') {
const apnsToken = await messaging().getAPNSToken();
console.log(apnsToken, '------');
if (apnsToken) {
await messaging().setAPNSToken(apnsToken);
console.log('APNs Token:', apnsToken);
} else {
console.log('Failed to get APNs token');
}
}
const fcmToken = await messaging().getToken();
console.log(fcmToken, '-----');
//toaster.custom(fcmToken, fcmToken);
if (fcmToken) {
setFcmToken(fcmToken);
console.log('FCM Token:', fcmToken);
} else {
console.error('Failed to get FCM token');
}
}
}
useEffect(() => {
requestUserPermission();
}, []);
useEffect(() => {
notifee.onBackgroundEvent(async ({ type, detail }) => {
console.log('background', type, detail);
switch (type) {
case EventType.PRESS:
console.log(
'send in data such as feed or other name and based on that redirect to post or screen',
);
break;
}
});
notifee.onForegroundEvent(async ({ type, detail }) => {
console.log('foreground', type, detail);
switch (type) {
case EventType.PRESS:
console.log(
'send in data such as feed or other name and based on that redirect to post or screen',
);
break;
}
});
}, []);
useEffect(() => {
messaging().onNotificationOpenedApp(async remoteMessage => {
console.log('on notification opened app', remoteMessage);
onMessageReceived(remoteMessage);
});
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
onMessageReceived(remoteMessage);
});
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('on message', remoteMessage);
onMessageReceived(remoteMessage);
});
return unsubscribe;
}, []);
const onMessageReceived = (message: FirebaseMessagingTypes.RemoteMessage | undefined) => {
Platform.OS === 'android' &&
Toast.show({
type: 'customNotification',
props: { text1: `${message?.notification?.body}` },
position: 'top',
swipeable: true,
autoHide: false,
onPress: () => {
Toast.hide();
console.log('on press, redirect to page');
},
});
const notifeeData = {
body: message?.notification?.body,
data: message?.data,
android: {
channelId: 'default',
actions: [
{
title: 'Mark as Read',
pressAction: { id: 'read' },
},
],
// smallIcon: 'ic_large_icon',
color: '#FFDE18',
badgeIconType: AndroidBadgeIconType.LARGE,
importance: AndroidImportance.HIGH,
pressAction: {
id: 'read',
},
},
ios: {
foregroundPresentationOptions: {
badge: true,
sound: true,
banner: true,
list: true,
},
actions: [
{
id: 'mark-as-read',
title: 'Mark as Read',
},
],
},
};
notifee.displayNotification(notifeeData);
};