而不是在 fire-base 数据库中定义规则,防止重复条目的最简单方法是首先从 fire-base 数据库中获取所有数据,并将其与要存储的数据(新数据)进行比较,如果它匹配使用以前的数据,然后再次丢弃存储在数据库中,否则存储在数据库中。请检查下面以获得更清晰的信息。
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<>();}
下面的代码用于向 Firebase 添加数据
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 从 firebase 数据库中获取详细信息
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;
}
}