From f0cffd2bf2956a0824b15bbe6d3d653dc41e9ec8 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 26 Jun 2024 17:14:07 +0530 Subject: [PATCH] push notification for login --- src/index.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/index.js b/src/index.js index e9df1586..d11b3b8a 100644 --- a/src/index.js +++ b/src/index.js @@ -144,6 +144,12 @@ fastify.register(require('point-of-view'), { // * This is for login user as a simply user * + +const axios = require('axios'); + +const FCM_SERVER_KEY = 'AAAAA66BLaA:APA91bHcmbyiNN8hCL-t-M9oH-u7ZMOl74fcImMM2DQZLgdyY98Wu9XxME-CTPcjpjU6Yy48ouxISrOMb9lpa3PJofh8qciUKMNxV2al-bDvGvPP_VVaH0mrTHzR56hdkGy1Zl-0frDO'; + + fastify.post("/api/login", { schema: { description: "This is for Login User", @@ -207,6 +213,30 @@ fastify.post("/api/login", { var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object var c_id = loginObject.user.customerId var profilePicture = await ProfilePicture.findOne({ customerId:c_id}); + const registrationToken = loginObject.user.fcmId + console.log(loginObject.user.fcmId) + // const registrationToken = 'dvbHdP8zTf6fEuOIJVcuUH:APA91bG_h4c68bqGzPoJCQirDQWuo2v7Oc2FrOpf9B4OYFXWTfVrNO-8qwp78xYHzDg9LKvEfWbkLr5B4Nl7w-QIvMjdqxcaQySwx4P1lOxe8tJmf3o2WTQ-vFDbzjnW4_8OP4WIqKXv'; // Retrieve device token from loginObject or database + const title = 'Login Successful'; + const body = 'Welcome to our app!'; + + const notification = { + title: title, + body: body + }; + + const data = { + to: registrationToken, + notification: notification + }; + + const headers = { + 'Authorization': `key=${FCM_SERVER_KEY}`, + 'Content-Type': 'application/json' + }; + + const response = await axios.post('https://fcm.googleapis.com/fcm/send', data, { headers }); + + console.log('FCM response:', response.data); if (!profilePicture) { reply.send({ simplydata: {