Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

Firebase Cloud Function not executing Flutter

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.

question from:https://stackoverflow.com/questions/65933479/firebase-cloud-function-not-executing-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...