Created
January 24, 2024 22:52
-
-
Save uguraktas/340fb6db796306c105ecc0752fa5e85f to your computer and use it in GitHub Desktop.
Firebase NotificationService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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