const express = require('express'); const path = require('path'); const {google} = require('googleapis'); var admin = require("firebase-admin"); var SCOPES = 'https://www.googleapis.com/auth/firebase.messaging'; const port = 3333; var dirPath = path.join(__dirname , 'fcm_key'); var keyPath = require('./keypath.json'); for(key in keyPath){ var data = keyPath[key]; var jsonPath = path.join(dirPath , data.PATH); var adminAuthToken = require(jsonPath); admin.initializeApp({ credential: admin.credential.cert(adminAuthToken), databaseURL: data.DB_URL },key); } const app = express(); app.use(express.json({limit: '50mb'})); app.use(express.urlencoded({limit: '50mb' ,parameterLimit: 1000000 , extended : true})); app.post('/sendFcm',async function(req,res){ var dataReq = req.body; var application = dataReq.application; var fcm = admin.app(application); var tokens = ["ckATQ13PdBc:APA91bF_RbWjE8gFcWoWqyRwLuh7GtDyQnx-l3DCVWyfEyWCXBgMWeK6Cca4FW2Wjltgwd5Sck7kI-0L9vR0i1MwRMbqu1--2AaMFaU9G6cwbkt0DCGI8nJ-JpMmShjA1kMHDMa-8BUI", "e3S17M3ZMjU:APA91bEEb2otaSUV7Vu0hRxVCiCawxHXnc1E_PV1nxvSJOSxAL90SYGIOy_zbrsBJP-ewD0OgRQ44rfFDHF_ROQj3GKH8T5DfhUHeb4xPeVWc16kyF_NRyUvx3G_RDqgX4RYMKjfg6gs"] var message = { tokens : tokens, notification :{ title: 'MAKANM KANA', body: 'EVERIBADINUW.' }, data : { "click_action":"FLUTTER_NOTIFICATION_CLICK", "message_id":"1", "route":"/Pilihan", "type":"NOTIFICATION" } } fcm.messaging().sendMulticast(message) .then((response) => { console.log(response); if (response.failureCount > 0) { const failedTokens = []; response.responses.forEach((resp, idx) => { console.log('AAAA'); if (!resp.success) { failedTokens.push(registrationTokens[idx]); } }); console.log('List of tokens that caused failures: ' + failedTokens); } return res.status(200).json({"STATUS" : "SUKSES" ,"DATA" : response}); }).catch((error) => { console.log('ERROR'); return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error}); }); // var token = dataReq["to"]; // if(token.length > 0){ // // One Device // if(typeof token == 'string'){ // var message = { // token : token, // notification : dataReq.notification, // data : dataReq.data // } // fcm.messaging().send(message) // .then((response) => { // // Response is a message ID string. // return res.status(200).json({"STATUS" : "SUKSES" , "DATA" : response}); // }) // .catch((error) => { // return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error}); // }); // } // // Multi Devices // else{ // console.log(token); // var tokens = ["e3S17M3ZMjU:APA91bEEb2otaSUV7Vu0hRxVCiCawxHXnc1E_PV1nxvSJOSxAL90SYGIOy_zbrsBJP-ewD0OgRQ44rfFDHF_ROQj3GKH8T5DfhUHeb4xPeVWc16kyF_NRyUvx3G_RDqgX4RYMKjfg6gs", // "dN1qcm6jr9M:APA91bFuXr5tx0tVqKYqGxWDjaNm8NrbzO2hFpK6ylQHxQzZMnTjN4GtT3MNOXRkU-Mj98A9xMmJy715IQJX_aQb_7PqmzNchegeDOTJn86zcnTMlkqU6IKJaFkU2CRsZU_bsHXDeDoX"] // var message = { // tokens : tokens, // notification : data.notification, // data : dataReq.data // } // fcm.messaging().sendMulticast(message) // .then((response) => { // console.log(response); // if (response.failureCount > 0) { // const failedTokens = []; // response.responses.forEach((resp, idx) => { // console.log('AAAA'); // if (!resp.success) { // failedTokens.push(registrationTokens[idx]); // } // }); // console.log('List of tokens that caused failures: ' + failedTokens); // } // return res.status(200).json({"STATUS" : "SUKSES" ,"DATA" : response}); // }).catch((error) => { // console.log('ERROR'); // return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error}); // }); // } // } }); app.listen(port,()=> console.log(`Listening into port ${port}`));