Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...