瀏覽代碼

Simplify Send FCM

master
Frallatta 4 年之前
父節點
當前提交
b107209f7a
共有 1 個文件被更改,包括 46 次插入113 次删除
  1. +46
    -113
      index.js

+ 46
- 113
index.js 查看文件

@@ -1,127 +1,60 @@
const express = require('express');
const path = require('path');
const {google} = require('googleapis');
const express = require("express");
const path = require("path");
var admin = require("firebase-admin");
var SCOPES = 'https://www.googleapis.com/auth/firebase.messaging';
const port = 3004;

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);


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);
app.use(
express.json({ limit: "50mb", parameterLimit: 1000000, extended: true })
);
app.use(
express.urlencoded({ limit: "50mb", parameterLimit: 1000000, extended: true })
);

app.post("/sendFcm", async function (req, res) {
try {
var message = req.body;
var fcm = admin.app(application.application);

fcm
.messaging()
.sendMulticast(message)
.then((response) => {
if (response.failureCount > 0) {
const failedTokens = [];
response.responses.forEach((resp, idx) => {
console.log('AAAA');
const failedTokens = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(registrationTokens[idx]);
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});
return res.status(200).json(response);
})
.catch((error) => {
return res.status(404).json(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});
// });
// }
// }




} catch (e) {
console.log(e.message);
return res.status(404).json(e.message);
}
});

app.listen(port,()=> console.log(`Listening into port ${port}`));
app.listen(port, () => console.log(`Listening into port ${port}`));

Loading…
取消
儲存