Napisałem aplikację na Androida która wyświetla dane z Firebase.
Aktywność główna to button uruchamiający drugą aktywność.
Druga aktywność to RecyclerView w któym są wyświetlane CardView, w każdym CardView dane z Firebase (Tytuł wiadomości, treść wiadomości, data dodania)
Dodatkowo napisałem Service który wyświetla push notyfikację gdy do Firebase zostaną dodane nowe dane.
Wszystko działa świetnie ale...
Przy każdym uruchomieniu aplikacji wyświetlana jest push notyfikacja, gdy usuwam jakiś rekord z Firebase również. Chciałbym aby notyfikacja wyświetlała się tylko i wyłacznie w przypadku dodania nowego rekordu do Firebase.
Główna aktywność
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService();
ref = FirebaseDatabase.getInstance().getReference().child("SM");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
utworzNotyfikacje();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
ref.addValueEventListener(valueEventListener);
button = (Button) findViewById(R.id.openActivity2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
//startService();
}
});
}
public void startService(){
Intent intent = new Intent(this, MyFirebaseMessagingService.class);
startService(intent);
}
}
Service
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
/**
* Created by Piotr on 2017-02-25.
*/
public class MyFirebaseMessagingService extends Service{
private DatabaseReference ref;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ref = FirebaseDatabase.getInstance().getReference().child("SM");
ValueEventListener valuEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
utworzNotyfikacje();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
ref.addValueEventListener(valuEventListener);
return START_STICKY;
}
@Override
public void onDestroy() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void utworzNotyfikacje() {
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("Nowa wiadomość")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
Jakies pomysły jak to rozwiązać?
Dodam tylko, że jestem baaardzo początkujący ;)
Z góry dziękuję za pomoc :)