Skip to content

Instantly share code, notes, and snippets.

@thanujaa-vb
Last active May 24, 2023 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanujaa-vb/754b7dc9a5c76faa521718d7706d2e50 to your computer and use it in GitHub Desktop.
Save thanujaa-vb/754b7dc9a5c76faa521718d7706d2e50 to your computer and use it in GitHub Desktop.
Firebase Notifications
{
"expo": {
"android": {
"googleServicesFile": "./google-services.json"
},
"ios": {
"googleServicesFile": "./GoogleService-Info.plist"
},
"plugins": [
"@react-native-firebase/app",
"@react-native-firebase/messaging"
]
}
}
import messaging from '@react-native-firebase/messagig';
messaging().getToken().then((token) => {
console.log(token);
});
const admin = require('firebase-admin');
const serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
module.exports = admin;
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);
});
import messaging from '@react-native-firebase/messagig';
messaging().requestPermission().then((permission) => {
if(permission) {
console.log('Permission granted');
} else {
console.log('Permission denied');
}
});
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