2016-11-18 37 views
0

我們必須使用Play框架實施帶有服務器的Android應用,並且必須使用Firebase進行服務器到客戶端的通信。在客戶端,我們已經成功實現了Firebase(我希望至少)。但是,我們不知道如何在服務器端實現Firebase消息傳遞,並且Firebase文檔無法幫助我們。在Play框架中實現Firebase

我們正在使用最新版本的Play。

如果有人能夠向我們解釋實施過程,那將會很棒。

回答

0

我已經非常接近完全實現Java Play上的firebase了!我遵循這裏的步驟:https://firebase.google.com/docs/admin/setup

1)我手動添加使用jar下載(dot)COM /在線maven-download-tool.php的Firebase jar。我手動完成了,因爲build.sbt沒有下載罐子。

<dependency> 
    <groupId>com.google.firebase</groupId> 
    <artifactId>firebase-admin</artifactId> 
    <version>4.0.1</version> 
</dependency> 

2)從控制檯獲取服務帳戶JSON。 firebase.google.com/

3)的代碼添加到一個單在你玩的項目

FirebaseOptions options = new FirebaseOptions.Builder() 
    .setServiceAccount(new FileInputStream("path/to/serviceAccountKey.json")) 
    .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/") 
    .build(); 

FirebaseApp.initializeApp(options); 

4)檢索數據

public static class Post { 

    public String author; 
    public String title; 

    public Post(String author, String title) { 
     // ... 
    } 

} 

// Get a reference to our posts 
final FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts"); 

// Attach a listener to read the data at our posts reference 
ref.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     Post post = dataSnapshot.getValue(Post.class); 
     System.out.println(post); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     System.out.println("The read failed: " + databaseError.getCode()); 
    } 
}); 

這是據我已經得到了。我能夠僅檢索一次數據,然後其餘的調用只是掛起而不會返回或錯誤。如果您有更多的成功,請對此問題或我的問題發表評論。 Firebase and Java Play! Framework not fully working together