而不是定義在消防基礎數據庫中的規則,以防止重複條目,最簡單的方法是首先從消防基礎數據庫中獲取的所有數據,並將其與數據進行比較(新的數據),你想存儲,如果它與以前的數據匹配,然後再放棄存儲在數據庫中,否則存儲在database.check下面更清晰。
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private BroadcastReceiver mRegistrationBroadcastReceiver;
private TextView txtRegId, txtMessage;
DatabaseReference databaseArtists;
ListView listViewArtists;
public static String regId;
List<Artist> artistList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtRegId = (TextView) findViewById(R.id.regid);
txtRegId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayFirebaseRegId();
boolean flag=false;
String tokenId=regId;
for(Artist a:artistList)
{Log.d("RAaz",a.getTokenId()+" "+tokenId);
if(a.getTokenId().equalsIgnoreCase(tokenId))
{
flag=true;
Toast.makeText(MainActivity.this, "True", Toast.LENGTH_SHORT).show();
}
}
if(flag)
{
Toast.makeText(MainActivity.this, "User Already Exists", Toast.LENGTH_SHORT).show();
}
else {
addArtist();
}
}
});
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
databaseArtists = FirebaseDatabase.getInstance().getReference("artist");
artistList = new ArrayList<>();}
下面的代碼是用於將數據添加到火力
private void addArtist() {
String name = "User";
String genre = regId;
if (!TextUtils.isEmpty(name)) {
String id = databaseArtists.push().getKey();
Artist artist = new Artist(id,genre,name);
databaseArtists.child(id).setValue(artist);
Toast.makeText(this, "Artist Added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please enter name", Toast.LENGTH_SHORT).show();
}
}
使用在onStart從火力數據庫中獲取的細節
protected void onStart() {
super.onStart();
Toast.makeText(this, "On Start", Toast.LENGTH_SHORT).show();
databaseArtists.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
artistList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Artist artist = dataSnapshot1.getValue(Artist.class);
artistList.add(artist);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
最後加POJO類
public class Artist {
private String artistId;
private String tokenId;
private String roleName;
public Artist() {
}
public Artist(String artistId, String tokenId, String roleName) {
this.artistId = artistId;
this.tokenId = tokenId;
this.roleName = roleName;
}
public String getArtistId() {
return artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public String getTokenId() {
return tokenId;
}
public void setTokenId(String tokenId) {
this.tokenId = tokenId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
這裏有兩件事:我不認爲這會解決我的問題,以防相同的用戶被添加兩次。我的問題是,我不希望兩個相同的條目僅因ID而不同。 – Retsam
在附註上,我避免使用push的原因是因爲它提供非數字ID,因爲它必須與假定用戶ID是數字的其他組件良好地配合使用。 – Retsam
啊,明白了。您需要使用.transaction()來分配唯一的ID。這裏有一個例子:https://gist.github.com/anantn/4323981 – Anant