[英]For my android app , How to watch or filter Firebase event for specific user ID and How to get all all users who have triggered a event
[英]Firebase: Android : How to show list of contacts who have the app Installed
我正在通過 firebase 使用 Google 身份驗證並成功登錄用戶。 我還從電話簿(設備)中檢索了聯系人列表,並將其顯示在我應用程序片段的列表視圖中。 但現在我希望在我的聯系人中顯示安裝了我的應用程序的用戶,這樣當點擊他們時他們將進入與他們的私人聊天,其他聯系人在點擊時將使他們能夠發送應用程序邀請。 簡而言之:我想查看在其設備上安裝了該應用程序的聯系人列表。
我能夠通過三個簡單的步驟實現這一目標。
為了使用我的方法,您需要在 Firestore 上有一個集合,其中包含所有用戶的電話號碼作為文檔,如下圖所示:
以下是步驟:
第 1 步:我使用 ContentResolver 獲得了所有用戶聯系人的列表。 如果您已授予READ_CONTACTS
權限,則可以使用以下方法檢索此列表。
public ArrayList<String> getContacts(ContentResolver cr) {
// list to be returned
ArrayList<String> numbers = new ArrayList<>();
// cursor
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
numbers.add(formatRightWay(phoneNo));
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
return numbers;
}
第 2 步:通過獲取user
集合的文檔 ID,我得到了 Firestore 上所有電話號碼的列表。 這是一個快速實現:
firestore.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}
// this is the list you need
Log.d(TAG, list.toString());
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
第 3 步:編寫一個方法來比較兩個列表並返回相似的元素。
public static ArrayList<String> shuffleBothLists(ArrayList<String> phoneContacts, List<String> firebaseContacts) {
ArrayList<String> result = new ArrayList<>();
for(String s: firebaseContacts) {
if(phoneContacts.contains(s) && !result.contains(s)) {
result.add(s);
}
}
return result;
}
上述方法返回的列表是您安裝了該應用程序的聯系人。
干杯!
無法直接列出聯系人。 您需要在 firebase 數據庫中為用戶創建一個節點來存儲注冊后的用戶詳細信息,然后您可以檢索這些用戶詳細信息。
我讓你進來意味着你正在使用 firebase。 現在你想通過你的應用程序將所有聯系人上傳到你在 firebase 數據庫中的服務器(如果安裝在你的設備中)。
試試下面的代碼:
public class YourActivity extends AppCompatActivity {
ProgressDialog dialog;
DatabaseReference yourReference;//your database reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yourReference = FirebaseDatabase.getInstance().getReference().child("users");
setContentView(R.layout.activity_your);
dialog = new ProgressDialog(this);
dialog.setMessage("Uploading contacts...");
// Query for contacts through content resolver. You will get a cursor.
Cursor contacts = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
},
null,
null,
null
);
// If you have a list as your data, firebase facilitates you to upload that easily by a single HashMap object. Create a HashMap object.
HashMap<String,Object> map = new HashMap<>();
// Loop contacts cursor with map to put all contacts in map. I used contact name as key and number as its value (simple and pretty way).
if(contacts!=null) {
while(contacts.moveToNext()){
map.put(
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
);
}
contacts.close();
}
dialog.show();
//write map to firebase database reference...
yourReference.updateChildren(map)
//this onSuccessListener is optional. You can terminate above line of code by ";" (semicolon).
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
dialog.dismiss();
Toast.makeText(YourActivity.this, "Contacts uploaded suffessfully!", Toast.LENGTH_SHORT).show();
}
})
//this onFailureListener is also optional.
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Log.w("MKN","Error: "+e.getMessage());
Toast.makeText(YourActivity.this, "Contacts upload failed.", Toast.LENGTH_SHORT).show();
}
});
}
}
您需要提供 READ_CONTACTS 權限才能查詢聯系人表。 同樣在 firebase 規則中,“write”鍵的值必須為“true”才能寫入數據庫。
首先檢索聯系人列表..
'ContentResolver cr = getContext().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
HashMap<String, String> map = new HashMap<>();
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if( email != null ){
map.put("name", name);
map.put("email", email);
getContactList.add(map);
}
}
cur1.close();
}
}'
在此之后,您可以維護一個可以存儲經過身份驗證的用戶信息的 firebase 數據庫表,您可以將您的聯系人與從 firebase 用戶數據庫中獲取的列表同步。
'mapChat = new HashMap<>();
Log.d("Debug", clist.toString());
userReference1 = FirebaseDatabase.getInstance().getReference().child("Users");
userReference1.keepSynced(true);
userReference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (int x = 0; x < clist.size(); x++) {
//Log.d("Debug" ,list.get(x).get("email").toString());
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
if (dsp.hasChild("email")) {
// Log.d("Debug" , "setnewuser " + dsp.child("email").getValue().toString());
if (dsp.child("email").getValue().toString().equals(clist.get(x).get("email").toString())) {
Log.d("Debug", "contact updated");
String uid = dsp.getKey().toString();
reference1 = FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("contacts").child(uid);
mapChat.put("name", clist.get(x).get("name"));
mapChat.put("email", clist.get(x).get("email"));
mapChat.put("chats", "false");
reference1.setValue(mapChat);
}
}
}
}
reference1.onDisconnect();
contactIdInterface1.contactUpdated();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});'
在 firebase 中啟用並實現電話號碼登錄方法,這樣您就可以從 firebase 中檢索聯系人並將其與本地聯系人列表進行比較,這很容易實現您的邏輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.