首页 > 解决方案 > Firebase函数模拟器不适用于android

问题描述

我已经尝试了 3 天的不同解决方案,无法弄清楚。这是我的代码:

FirebaseLogic.java

public class FirebaseLogic {

    // Access a Cloud Firestore instance from your Activity
    static FirebaseFirestore db ;

    //Firebase functions instance
    static FirebaseFunctions mFunctions;

    // Create a storage reference from our app
    static FirebaseStorage storage ;
    static StorageReference storageRef ;


    public static void init()
    {
        //////////Remove this if you re not working with emulator anymore
        FirebaseFirestore firestore = FirebaseFirestore.getInstance();
        firestore.useEmulator("10.0.2.2", 8080);


        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .build();
        firestore.setFirestoreSettings(settings);

        mFunctions = FirebaseFunctions.getInstance();
        mFunctions.useEmulator("10.0.2.2.", 5001);

        //////////////////////////////////////////////////////

        // Access a Cloud Firestore instance from your Activity
        db = FirebaseFirestore.getInstance();


        // Create a storage reference from our app
        storage = FirebaseStorage.getInstance();
        storageRef = storage.getReference();



    }
public static Task<String> addMessage(String text) {
        // Create the arguments to the callable function.
        Map<String, Object> data = new HashMap<>();
        data.put("text", text);
        data.put("push", true);

        return mFunctions
                .getHttpsCallable("addMessage")
                .call(data)
                .continueWith(new Continuation<HttpsCallableResult, String>() {
                    @Override
                    public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                        // This continuation runs on either success or failure, but if the task
                        // has failed then getResult() will throw an Exception which will be
                        // propagated down.
                        String result = (String) task.getResult().getData();
                        Log.w("messi", result);


                        return result;
                    }
                });
    }
}

Activity.java(这就是我调用firebase函数只是为了测试的方式)

//...
Button button = (Button) root.findViewById(R.id.BtnAddImages);
button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FirebaseLogic.init();
                FirebaseLogic.addMessage("hmida").addOnCompleteListener(new OnCompleteListener<String>() {
                    @Override
                    public void onComplete(@NonNull Task<String> task) {
                        if (!task.isSuccessful()) {
                            Exception e = task.getException();
                            if (e instanceof FirebaseFunctionsException) {
                                FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                                FirebaseFunctionsException.Code code = ffe.getCode();
                                Object details = ffe.getDetails();
                            }

                            Log.w("functions", "No Success");
                        }
                        if(task.isSuccessful())
                        {
                            Log.w("functions", "Success");
                        }

                        // ...
                    }
                });
            }
        });
            });
//...

index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.addMessage = functions.https.onCall((data, context) => {
return "hello";
});

顺便说一句,firestore 工作正常,我只有函数有问题,我已经在 gradle 文件中添加了实现,所以不是这样我试图记录异常消息,但他们没有提供任何有用的信息,他们只是说“内部的”

标签: javaandroidnode.jsgoogle-cloud-functionsfirebase-tools

解决方案


推荐阅读