Skip to content

Instantly share code, notes, and snippets.

@uguraktas
Created January 24, 2024 22:52
Show Gist options
  • Save uguraktas/340fb6db796306c105ecc0752fa5e85f to your computer and use it in GitHub Desktop.
Save uguraktas/340fb6db796306c105ecc0752fa5e85f to your computer and use it in GitHub Desktop.
Firebase NotificationService
import messaging from '@react-native-firebase/messaging';
export async function registerForPushNotifications(uid) {
// Check and request permissions
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
// Get the device token
const token = await messaging().getToken();
// App is open and in the foreground
messaging().onMessage(async remoteMessage => {
console.log('onMessage:', remoteMessage);
// handle your message
});
// App is in the background
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('setBackgroundMessageHandler', remoteMessage);
// handle your message
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment