Send push notifications using Triggers and FCM

Hello everyone. Could someone tell how I can write a function to send a push notification from an insert/update Trigger. The trigger will watch over Order collection. My FCM tokens are stored inside UserInfo collection, “FCMToken” field. The idea is when a new Order is created, the correct user will receive a push notification. My app is KMM (Kotlin+SwiftUI).

You have to create a Trigger on the collection you want the trigger to be.

There you write a function. The function receives the changeEvent as an parameter. So from the object itself you can now access the user that you want to send the notification to. It must be in someway stored in the document.

Not all you have to do is send a notification using the firebase-admin library.

1 Like

I am doing this. I dont know how I can use the firebase library. Could you give me an example.

You have to add firebase-admin as a dependency. I am using verdion 9.7.0.

const firebase_credentials = context.values.get("firebase_credentials");
 const doc = JSON.parse(firebase_credentials);
 admin.initializeApp({
     credential: admin.credential.cert(doc),
 });
 await admin.messaging().send(message);

What I have to put inside firebase_credentials value?