Reputation: 3
I'm using React Native with Expo. I created the project using create-react-native-app
command.
I'd like to send remote notifications using Firebase Cloud Messaging for android device without going through expo's server.
eject
is the only way?
It's ok only for android.
reference: https://docs.expo.io/versions/v27.0.0/guides/push-notifications https://docs.expo.io/versions/v27.0.0/guides/using-fcm
Upvotes: 0
Views: 912
Reputation: 11
Honestly, I configured Expo Notifications few hours ago, and I noticed that you can send push to Android both ways, using Expo Push tool but using Firebase as well: the only thing you have to do is store your ExpoToken in Firebase. You are not forced to eject from Expo.
In the flow asking permission to send notifications, you obtain from Expo a token, in a format like ExponentPushToken[i4uCznGitg$84t****a343].
You can save as an external component this code:
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted' || Platform.OS === 'android') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
//alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
} else {
// alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
export default registerForPushNotificationsAsync;
The obtained token can be stored in your db (if you want to use it further through Expo push), but you could send it to Firebase to store it. In your Firebase project you can divide Android and iOS, and saving those separately in different topics.
Do a test: obtain a ExpoPushToken, store it in Firebase, then send to it a test push, you will see that it works perfectly.
As explained in Expo docs (Using FCM for Push Notifications), if you want to use Expo to send notifications to Android, you must set it sending your Server Token
Upvotes: 0
Reputation: 374
The Expo guide for using FCM is here:https://docs.expo.io/versions/v29.0.0/guides/using-fcm
It says that FCM "is required for all new standalone Android apps made with Expo".
Upvotes: 0
Reputation: 914
Yes, you should eject it and then use some libraries like https://github.com/evollu/react-native-fcm (this is the one that I use), it is well documented.
If you do not want to use the library directly, you can use OneSignal or something like that.
Regards
Upvotes: 0