Last active
May 24, 2023 05:41
-
-
Save thanujaa-vb/754b7dc9a5c76faa521718d7706d2e50 to your computer and use it in GitHub Desktop.
Firebase Notifications
This file contains 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
{ | |
"expo": { | |
"android": { | |
"googleServicesFile": "./google-services.json" | |
}, | |
"ios": { | |
"googleServicesFile": "./GoogleService-Info.plist" | |
}, | |
"plugins": [ | |
"@react-native-firebase/app", | |
"@react-native-firebase/messaging" | |
] | |
} | |
} |
This file contains 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/messagig'; | |
messaging().getToken().then((token) => { | |
console.log(token); | |
}); |
This file contains 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
const admin = require('firebase-admin'); | |
const serviceAccount = require('path/to/serviceAccountKey.json'); | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
}); | |
module.exports = admin; |
This file contains 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/messagig'; | |
// Handle incoming notifications when app is in foreground | |
messaging().onMessage(async (remoteMessage) => { | |
console.log('Received foreground notification: ', remoteMessage); | |
}); | |
// Handle incoming notifications when app is in background | |
messaging().onNotificationOpenedApp(async (remoteMessage) => { | |
console.log('Received background notification: ', remoteMessage); | |
}); | |
// Handle incoming notifications when app is closed | |
messaging().getInitialNotification().then(async (remoteMessage) => { | |
console.log('Received closed app notification: ', remoteMessage); | |
}); |
This file contains 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/messagig'; | |
messaging().requestPermission().then((permission) => { | |
if(permission) { | |
console.log('Permission granted'); | |
} else { | |
console.log('Permission denied'); | |
} | |
}); |
This file contains 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
const admin = require('./initialize.js'); | |
const message = { | |
notification: { | |
title: 'New Notification', | |
body: 'This is a new notification', | |
}, | |
token: 'DEVICE_TOKEN', | |
}; | |
admin.messaging().send(message).then((response) => { | |
console.log('Successfully sent message: ', response); | |
}) | |
.catch((error) => { | |
console.log('Error sending message: ', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment