- /faraday_infra : Contains the React Native code
- /function: Contains the Firebase cloud Functions code
- Run the command `npm install` in faraday_infra
- Run the command `npx react-native run-android` in faraday_infra
- Go into function directory
- Run the command `npm install -g firebase-tools`
- Run the command `firebase login`
- Run the command `firebase init`
- Run the command `firebase deploy --only functions`
- go to function directory and then again go into the functions directory
- make the changes in the `index.js` file
app.post("/create-user", async (req, res)) => {}- This function is used to create a user in the database and sends an email to the user with a temporary password.
- after generating the password, it is stored in the database and the user is sent an email with the password.
- The user can now login with the email and temporary password.
- The user can then change the password by going to the profile page and clicking on the "Reset Password" button.
- You can change the temporary password length by changing the value of the variable
passwordLengthin theindex.jsfile. the default value is 10.
Further Uses of the cloud function:
- Once the the app moves forward, use this cloud function to send push-notifications to the user.
- create multiple endpoints for the same, and trigger them based on the user's actions.
- I will provide with the snippets now
For sending notifications to multiple devices
app.post('/new-booking', async (req, res) => {
try {
// Get the user tokens from Firestore
const message = {
notification: {
title: 'Title of the notification',
body: 'Body of the notification',
},
tokens: req.body.tokens,( FCM tokens sent from the client)
};
const response = await admin.messaging().sendMulticast(message);
console.log('Successfully sent message to user:', response);
res.status(200).send('Notification sent to users');
} catch (error) {
console.error('Error sending notification to user:', error);
res.status(500).send('Send fail');
}
});The multicast needs "tokens"
For sending notifications to single device
app.post('/end-point2', async (req, res) => {
try {
const message = {
notification: {
title: 'Title of the notification',
body: 'Body of the notification',
},
token: req.body.token,( FCM token sent from the client)
};
const response = await admin.messaging().send(message);
console.log('Successfully sent message to user:', response);
res.status(200).send('Notification sent to user');
} catch (error) {
console.error('Error sending user notification:', error);
res.status(500).send('Send fail');
}
});
The single device needs "token"cd function
- I have provided with the code to export the data from the FireStore database to a JSON file.
- The code is in the
export-data.jsfile.
- Run the command
node export-data.js
- I have provided with the code to import the data from a JSON file to the FireStore database.
- The code is in the
import-data.jsfile.
- Run the command
node import-data.js
How to generate one in 4 steps?
Step 1: Check if the file your_project\android\app\src\main\assets\index.android.bundle exists, if not create it.
Step 2: Go to the root of the project in the terminal and run the below command:
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
Step 3: Go to android directory:
cd android
Step 4: Now in this android folder, run this command
./gradlew assembleDebug
- There! you’ll find the apk file in the following path:
yourProject/android/app/build/outputs/apk/debug/app-debug.apk