Wednesday, December 25, 2024
HomeiOS DevelopmentShip IOS Notification on Firestore Database change utilizing Node.JS

Ship IOS Notification on Firestore Database change utilizing Node.JS


I’ve constructed an IOS App utilizing Swift which has a WhatsApp type messaging function utilizing Firebase Firestore Database & am making an attempt to arrange the notifications that the message receiver will get when a brand new message is saved to the database.

I efficiently put in Firebase messaging and am receiving the Gadget Token by the App Delegate. I can then use that token to ship a check message from the Firebase Messaging Console to the chosen system with no drawback.

So, I moved onto establishing Node.JS (please be light, I’m knew at this bit) and figured I’d get the check setup prefer it says within the Firebase Docs, initially simply listening for a brand new doc in my database & then performing a easy activity – on this case it was simply altering a reputation discipline to capitals. The code for that working is:

const {logger} = require("firebase-functions");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");

const {initializeApp} = require("firebase-admin/app");

initializeApp();


exports.makeuppercase = onDocumentCreated("/tester/{documentId}", (occasion) => {
  // Seize the present worth of what was written to Firestore.
  const identify = occasion.knowledge.knowledge().identify;

  // Entry the parameter `{documentId}` with `occasion.params`
  logger.log("Uppercasing", occasion.params.documentId, identify);

  const uppercase = identify.toUpperCase();

  // It's essential to return a Promise when performing
  // asynchronous duties inside a perform
  // similar to writing to Firestore.
  // Setting an 'uppercase' discipline in Firestore doc returns a Promise.
  return occasion.knowledge.ref.set({uppercase}, {merge: true});
});

My closing check activity to place all of it collectively (& then write the true code) is to now get a check message to set off when that identify will get modified to Uppercase. I’ve learn/watched so many tutorials however nothing I do appears to work. My most up-to-date code included to the above is:

exports.sendListenerPushNotification = onDocumentCreated("/tester/{documentId}", (occasion) => {
  const identify = occasion.knowledge.knowledge().identify;  
  const FCM = require("fcm-node");
  const serverKey = require("MY_SERVER_KEY");
  const fcm = new FCM(serverKey);
  const message = {
    to: "SAME_DEVICE_ID_AS_TEST",
    collapse_key: "apns-collapse-id",
    notification: {
      title: "Title of your push notification",
      physique: "Physique of your push notification",
    },
  };
  fcm.ship(message, (err, response) => {
    if (err) {
      console.log("One thing has gone mistaken!");
    } else {
      console.log("Efficiently despatched with response: ", response);
    }
  });
});

The code deploys with out errors however no check notification is obtained. At this level, I’ve learn so many various issues that I’m confused as something! I’m very happy to learn documentation however, I discover this node.js/server stuff very complicated & something I appear to attempt would not work.

Might any skilled Dev clarify in easy phrases tips on how to take the subsequent step & ship that check notification when the identify modifications to uppercase?

Actually sorry if I’m am being silly & that is straightforward however, like I say, I’m a node.js beginner.

Many thanks upfront.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments