Firebase Cloud Messaging SDK Admin
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

128 lignes
4.5 KiB

  1. const express = require('express');
  2. const path = require('path');
  3. const {google} = require('googleapis');
  4. var admin = require("firebase-admin");
  5. var SCOPES = 'https://www.googleapis.com/auth/firebase.messaging';
  6. const port = 3004;
  7. var dirPath = path.join(__dirname , 'fcm_key');
  8. var keyPath = require('./keypath.json');
  9. for(key in keyPath){
  10. var data = keyPath[key];
  11. var jsonPath = path.join(dirPath , data.PATH);
  12. var adminAuthToken = require(jsonPath);
  13. admin.initializeApp({
  14. credential: admin.credential.cert(adminAuthToken),
  15. databaseURL: data.DB_URL
  16. },key);
  17. }
  18. const app = express();
  19. app.use(express.json({limit: '50mb'}));
  20. app.use(express.urlencoded({limit: '50mb' ,parameterLimit: 1000000 , extended : true}));
  21. app.post('/sendFcm',async function(req,res){
  22. var dataReq = req.body;
  23. var application = dataReq.application;
  24. var fcm = admin.app(application);
  25. var tokens = ["ckATQ13PdBc:APA91bF_RbWjE8gFcWoWqyRwLuh7GtDyQnx-l3DCVWyfEyWCXBgMWeK6Cca4FW2Wjltgwd5Sck7kI-0L9vR0i1MwRMbqu1--2AaMFaU9G6cwbkt0DCGI8nJ-JpMmShjA1kMHDMa-8BUI",
  26. "e3S17M3ZMjU:APA91bEEb2otaSUV7Vu0hRxVCiCawxHXnc1E_PV1nxvSJOSxAL90SYGIOy_zbrsBJP-ewD0OgRQ44rfFDHF_ROQj3GKH8T5DfhUHeb4xPeVWc16kyF_NRyUvx3G_RDqgX4RYMKjfg6gs"]
  27. var message = {
  28. tokens : tokens,
  29. notification :{
  30. title: 'MAKANM KANA',
  31. body: 'EVERIBADINUW.'
  32. },
  33. data : {
  34. "click_action":"FLUTTER_NOTIFICATION_CLICK",
  35. "message_id":"1",
  36. "route":"/Pilihan",
  37. "type":"NOTIFICATION"
  38. }
  39. }
  40. fcm.messaging().sendMulticast(message)
  41. .then((response) => {
  42. console.log(response);
  43. if (response.failureCount > 0) {
  44. const failedTokens = [];
  45. response.responses.forEach((resp, idx) => {
  46. console.log('AAAA');
  47. if (!resp.success) {
  48. failedTokens.push(registrationTokens[idx]);
  49. }
  50. });
  51. console.log('List of tokens that caused failures: ' + failedTokens);
  52. }
  53. return res.status(200).json({"STATUS" : "SUKSES" ,"DATA" : response});
  54. }).catch((error) => {
  55. console.log('ERROR');
  56. return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error});
  57. });
  58. // var token = dataReq["to"];
  59. // if(token.length > 0){
  60. // // One Device
  61. // if(typeof token == 'string'){
  62. // var message = {
  63. // token : token,
  64. // notification : dataReq.notification,
  65. // data : dataReq.data
  66. // }
  67. // fcm.messaging().send(message)
  68. // .then((response) => {
  69. // // Response is a message ID string.
  70. // return res.status(200).json({"STATUS" : "SUKSES" , "DATA" : response});
  71. // })
  72. // .catch((error) => {
  73. // return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error});
  74. // });
  75. // }
  76. // // Multi Devices
  77. // else{
  78. // console.log(token);
  79. // var tokens = ["e3S17M3ZMjU:APA91bEEb2otaSUV7Vu0hRxVCiCawxHXnc1E_PV1nxvSJOSxAL90SYGIOy_zbrsBJP-ewD0OgRQ44rfFDHF_ROQj3GKH8T5DfhUHeb4xPeVWc16kyF_NRyUvx3G_RDqgX4RYMKjfg6gs",
  80. // "dN1qcm6jr9M:APA91bFuXr5tx0tVqKYqGxWDjaNm8NrbzO2hFpK6ylQHxQzZMnTjN4GtT3MNOXRkU-Mj98A9xMmJy715IQJX_aQb_7PqmzNchegeDOTJn86zcnTMlkqU6IKJaFkU2CRsZU_bsHXDeDoX"]
  81. // var message = {
  82. // tokens : tokens,
  83. // notification : data.notification,
  84. // data : dataReq.data
  85. // }
  86. // fcm.messaging().sendMulticast(message)
  87. // .then((response) => {
  88. // console.log(response);
  89. // if (response.failureCount > 0) {
  90. // const failedTokens = [];
  91. // response.responses.forEach((resp, idx) => {
  92. // console.log('AAAA');
  93. // if (!resp.success) {
  94. // failedTokens.push(registrationTokens[idx]);
  95. // }
  96. // });
  97. // console.log('List of tokens that caused failures: ' + failedTokens);
  98. // }
  99. // return res.status(200).json({"STATUS" : "SUKSES" ,"DATA" : response});
  100. // }).catch((error) => {
  101. // console.log('ERROR');
  102. // return res.status(404).json({"STATUS" : "ERROR" ,"DATA" : error});
  103. // });
  104. // }
  105. // }
  106. });
  107. app.listen(port,()=> console.log(`Listening into port ${port}`));