我知道如何使用signInWithCustomToken来启动,但是我不知道如何在Android中监听/观察文档更改。我知道如何在Angular中做到这一点,所以我将粘贴波纹管作为示例。
如果我关闭Firestore Rule Auth,我可以使用
获取文档val db = FirebaseFirestore.getInstance()
db.collection("transfer")
.get()
.addOnSuccessListener { result ->
for (document in result) {
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents.", exception)
}
但是我需要保持Firestore中的Auth正常运行
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, update, write: if request.auth.uid != null;
}
}
}
我知道如何使用CustomToken在Android中进行连接
lateinit var auth: FirebaseAuth// ...
auth = FirebaseAuth.getInstance()
auth.signInWithCustomToken("eyJhbGc
*** a valid customtoken *** Wi3KcvX4ILYN7kWySB4uuDtoNE_rIvXvD7VOpvCuLZ65d5lJHTBRhfAKJMiKyokQbWTZQ1GkQ")
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCustomToken:success")
val user = auth.currentUser
//updateUI(user)
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCustomToken:failure", task.exception)
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
//updateUI(null)
}
}
但是我看不到FirebaseAuth中的方法,该方法允许我利用Firestore的Real Database功能来设置我的收藏集以及侦听/听众/快照以进行更改。
只是为了举例说明我要在Android / Kotlin中进行的操作,这是我如何使用快照和自定义令牌通过Angular成功实现
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import 'rxjs/Rx';
import { AngularFireAuth } from '@angular/fire/auth';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
public transfers: Observable<any[]>;
transferCollectionRef: AngularFirestoreCollection<any>;
constructor(public auth: AngularFireAuth, public db: AngularFirestore) {
this.listenSingleTransferWithToken();
}
async listenSingleTransferWithToken() {
await this.auth.signInWithCustomToken("ey *** valid customtoken *** daG1Q");
this.transferCollectionRef = this.db.collection<any>('transfer', ref => ref.where("id", "==", "1"));
this.transfers = this.transferCollectionRef.snapshotChanges().map(actions => {
return actions.map(action => {
const data = action.payload.doc.data();
const id = action.payload.doc.id;
return { id, ...data };
});
});
}
***已编辑
***找到当前解决方案。由于这是我在Firestore / Android中的第一个项目,因此我们将不胜感激任何建议。顺便说一句,这正在工作:
package com.example.demo
//https://developer.android.com/training/basics/firstapp/starting-activity
//https://firebase.google.com/docs/firestore/quickstart#kotlin+ktx
// Tutorial to CustomToken
//https://firebase.google.com/docs/auth/android/custom-auth#kotlin+ktx
//Tutorial to Snatshot Listeners and its queries
//https://firebase.google.com/docs/firestore/query-data/listen#kotlin+ktx
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore
class MainActivity : AppCompatActivity() {
lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
val TAG = "MainActivity"
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
auth = FirebaseAuth.getInstance()
auth.signInWithCustomToken("eyJ **** uXa-CSuHUrg")
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
Log.d(TAG, "*** signInWithCustomToken:success")
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCustomToken:failure", task.exception)
Toast.makeText(
baseContext, "Authentication failed.",
Toast.LENGTH_SHORT
).show()
}
}
//val db = FirebaseFirestore.getInstance()
FirebaseFirestore.getInstance().collection("transfer")
.whereEqualTo("id", "1")
.addSnapshotListener { value, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@addSnapshotListener
}
val transfer = ArrayList<String>()
for (doc in value!!) {
doc.getString("status")?.let {
transfer.add(it)
}
}
// for (document in value) {
// Log.d(TAG, "${document.id} => ${document.data}")
// }
Log.d(TAG, "*** transfer: $transfer")
}
}
}
答案 0 :(得分:1)
当前可行的解决方案:
error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]