0

I have the following function in my index.ts file:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);

const db = admin.firestore();
const fcm = admin.messaging();


export const sendToDevice = functions.firestore
      .document('orders/{orderId}')
      .onCreate(async snapshot => {
        print("aa")
        console.log("osakosak");
        const order = snapshot.data();
    
        const querySnapshot = await db
          .collection('users')
          .doc(order.ustaID)
          .collection('tokens')
          .get();
    
        const tokens = querySnapshot.docs.map(snap => snap.id);
    
        const payload: admin.messaging.MessagingPayload = {
          notification: {
            title: 'New Order!',
            body: `you sold a ${order.day} for ${order.time}`,
            click_action: 'FLUTTER_NOTIFICATION_CLICK'
          }
        };
    
        return fcm.sendToDevice(tokens, payload);
      });

However, when the new document gets added into the order collection, this doesn't get triggered. Even the print and console.log don't work. I tried putting print and console log before export, and it still didn't fire.

10
  • Since version 1.0, you should initialize firebase-admin as folllows: admin.initializeApp(); Commented Jan 28, 2021 at 8:32
  • I changed it, but the outcome was the same @RenaudTarnec Commented Jan 28, 2021 at 8:33
  • You don't see console.log("osakosak");in the log? Commented Jan 28, 2021 at 8:34
  • Nope, not when the app starts nor when the document is created Commented Jan 28, 2021 at 8:37
  • Is the Cloud Function correctly deployed. What do you see in the Cloud Function console? In the Google Cloud console? Commented Jan 28, 2021 at 8:40

1 Answer 1

1

Based on your comments ("It depends on cloud_firestore in pubspec.yaml"), it seems that you didn't deploy your Cloud Function correctly.

As a matter of fact, Cloud Functions are totally independent from your Flutter app (your front-end). It is a back-end service. You should deploy it with the Firebase CLI, see the doc. Note that the code shall be in the Firebase Project, not in your Flutter project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.