2017-07-29 5 views
-1

firebase経由でGoogle認証を使用していて、ユーザーを正常にログインしています。私はまた、電話帳(デバイス)からの連絡先のリストを取得し、私のアプリの断片のリストビューに表示しています。しかし今、私の連絡先に私のアプリがインストールされているユーザーをに見せたいので、クリックしたときにそのユーザーとのプライベートチャットに進み、他の連絡先をクリックするとアプリの招待を送信できるようになります。一言で言えば、自分のデバイスにアプリがインストールされている連絡先のリストを表示したいのですが。Firebase:Android:アプリをインストールした連絡先のリストを表示するには

+2

ウルロジックを実装するのは容易ではあなたが持っているものを私たちに示してくださいことをした後、これまでのところ、あなたが直面している問題を試しました。 –

+0

あなたの連絡先リストには、アプリをインストールした人とインストールしていない人はどうですか?つまり、連絡先をすべて見た場合、連絡先にはアプリがインストールされているとはどういうことでしょうか? – Jay

答えて

0

連絡先を直接一覧表示することはできません。 firebaseデータベースのユーザ用に1つのノードを作成して、登録後にユーザの詳細を保存してから、そのユーザの詳細を取得する必要があります。

0

私はあなたが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(); 
     } 
    }); 
} 
} 

あなたはContactsテーブルを照会するためREAD_CONTACTS許可を提供する必要があります。 また、firebaseのルールでは、 "write"キーの値が "true"でデータベースに書き込む必要があります。

0

まず、あなたは、あなたがfirebaseユーザーのデータベースからフェッチリストと連絡先を同期することができ、認証されたユーザーの情報を格納することができますfirebaseデータベーステーブルを維持することができます。..この後

'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(); 
      } 
     }' 

をコンタクトリストを取得します。

'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) { 

      } 
     });' 
+0

Firebase認証ユーザーのリスト*を取得する方法を提案しますか?ソリューションを提供するコード例がありますか? – Jay

+0

これは質問に対する答えを提供しません。十分な[評判](https://stackoverflow.com/help/whats-reputation)があれば、[投稿にコメントする]ことができます(https://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの明確化を必要としない回答を提供する](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューの投稿](レビュー/低品質の投稿/ 16867971) –

+0

私は、これは解決策を提供していないと確信していますが、いくつかの連絡先にはアプリがインストールされていて、それは。また、クリックしてプライベートチャットルームに行く方法も提供していません。 – Jay

0

ので、uはfirebaseから連絡先を取得し、それを比較することができ、firebase方法で電話番号の記号を有効にして実装しますローカルの連絡先リスト

関連する問題