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.

61 lignes
1.5 KiB

  1. const express = require("express");
  2. const path = require("path");
  3. var admin = require("firebase-admin");
  4. const port = process.env.PORT || 3004;
  5. var dirPath = path.join(__dirname, "fcm_key");
  6. var keyPath = require("./keypath.json");
  7. for (key in keyPath) {
  8. var data = keyPath[key];
  9. var jsonPath = path.join(dirPath, data.PATH);
  10. var adminAuthToken = require(jsonPath);
  11. admin.initializeApp(
  12. {
  13. credential: admin.credential.cert(adminAuthToken),
  14. databaseURL: data.DB_URL,
  15. },
  16. key
  17. );
  18. }
  19. const app = express();
  20. app.use(
  21. express.json({ limit: "50mb", parameterLimit: 1000000, extended: true })
  22. );
  23. app.use(
  24. express.urlencoded({ limit: "50mb", parameterLimit: 1000000, extended: true })
  25. );
  26. app.post("/sendFcm", async function (req, res) {
  27. try {
  28. var message = req.body;
  29. var fcm = admin.app(application.application);
  30. fcm
  31. .messaging()
  32. .sendMulticast(message)
  33. .then((response) => {
  34. if (response.failureCount > 0) {
  35. const failedTokens = [];
  36. response.responses.forEach((resp, idx) => {
  37. if (!resp.success) {
  38. failedTokens.push(registrationTokens[idx]);
  39. }
  40. });
  41. }
  42. return res.status(200).json(response);
  43. })
  44. .catch((error) => {
  45. return res.status(404).json(error);
  46. });
  47. } catch (e) {
  48. console.log(e.message);
  49. return res.status(404).json(e.message);
  50. }
  51. });
  52. app.listen(port, () => console.log(`Listening into port ${port}`));