Firebase Java Admin SDK don't work

前端 未结 4 1306
暗喜
暗喜 2021-01-07 05:59

I am following the documentation on Firebase website on setting up an Java Admin SDK. So I add the dependency to the build.gradle, and added the following code:



        
4条回答
  •  渐次进展
    2021-01-07 06:58

    below code is pushing data into firebase database using java async call api from firebase sdk but listeners code is not executing . I am running below code in server side backend.

     public enum Firebase {
    INSTANCE;
    
    FirebaseApp firebaseApp;
    
    
    public void initilizeFirebaseApp(ConfigLoader configReader) {
    
        CountDownLatch done = new CountDownLatch(1);
        final AtomicInteger message1 = new AtomicInteger(0);
        InputStream firebaseSecret = getClass().getClassLoader().getResourceAsStream("ServiceAccount.json");
        final GoogleCredentials credentials;
        try {
            credentials = GoogleCredentials.fromStream(firebaseSecret);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error while reading Firebase config file." + e.toString());
            throw new IllegalStateException(e);
        }
    
        Map auth = new HashMap<>();
        auth.put("uid", "my_resources");
    
        FirebaseOptions options = new Builder()
                .setConnectTimeout(1000)
                .setCredentials(credentials)
                .setDatabaseAuthVariableOverride(auth)
                .setDatabaseUrl(configReader.getFirebaseDatabaseURL())
                .setStorageBucket(configReader.getFirebaseStorageBucket())
                .setProjectId(configReader.getFirebseProjectId())
                .build();
    
        firebaseApp = FirebaseApp.initializeApp(options);
    
    
        System.out.println(firebaseApp.getName());
        //System.out.println(firebaseApp.getName());
        // Use the shorthand notation to retrieve the default app's service
        FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
        FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
    
    
        // The app only has access as defined in the Security Rules
        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("/analyst_profiles");
    
    
        DateTime dt = new DateTime(java.util.Date.from(Instant.now()), java.util.TimeZone.getDefault());
        System.out.println(dt.getValue());
        //test data push
        // https://firebase.google.com/docs/database/admin/save-data
        AnalystProfiles analystProfilesObjTemp = new AnalystProfiles("test2", Long.toString(dt.getValue()), "dsds", "ds", "ds",
                "dsa2323", "32ddss232");
    
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                AnalystProfiles post = dataSnapshot.getValue(AnalystProfiles.class);
                System.out.println(post);
                //done.countDown();
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());
            }
    
    
        });
    
    
        CompletableFuture welcomeText = CompletableFuture.supplyAsync(() -> {
            try {
                ref.push().setValueAsync(analystProfilesObjTemp).get();
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException("Error while waiting for future", e);
            }
            return "ok";
    
        }).thenApply(x -> {
            System.out.println(x);
            System.out.println("Listeners code is not executing");
            return x;
        });
    
    
        done.countDown();
        try {
            System.out.println(welcomeText.get());
            done.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    
    
    }
    
    
    public void testdataLoad() {
    
        // The app only has access as defined in the Security Rules
        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("/analyst_profiles");
    
    
        DateTime dt = new DateTime(java.util.Date.from(Instant.now()), java.util.TimeZone.getDefault());
        System.out.println(dt.getValue());
        //test data push
        // https://firebase.google.com/docs/database/admin/save-data
        AnalystProfiles analystProfilesObjTemp = new AnalystProfiles("test2", Long.toString(dt.getValue()), "dsds", "ds", "ashutsh",
                "dsa2323", "32ddss232");
    
        CompletableFuture welcomeText = CompletableFuture.supplyAsync(() -> {
            try {
                ref.push().setValueAsync(analystProfilesObjTemp).get();
            } catch (ExecutionException | InterruptedException e) {
                throw new RuntimeException("Error while waiting for future", e);
            }
            return "ok";
    
        }).thenApply(x -> {
            System.out.println(x);
            return x;
        });
    
    
    
        try {
            System.out.println(welcomeText.get());
    
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    
    }
    

    }

提交回复
热议问题