firebase函数仿真器不适用于android

ymzxtsji  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(231)

我已经试了三天不同的方法了,想不出来。这是我的密码:
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;
                    }
                });
    }
}

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");
                        }

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

索引.js

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

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

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题