I am trying to create an android app where a notification appears whenever a firebase child is added to my JSON, I am able to get notifications only when I open the app, or when I close it recently. I am having some issues with it. 1) Whenever newly installed, I get notifications for all the child items that were previously added. I only want the freshly added child notifications. 2) I want to get notifications even when the app is not running, i.e, in background. Can someone please help me create a service for this, so that I can get this working.
Please also help me out with the below code, if I am doing something that doesn't make sense.
package com.fayaz.firebasenotify;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import com.google.firebase.database.ChildEventListener;
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 java.util.Random;
public class MainActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("user");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
int notify=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent notificationIntent = new Intent(this, MainActivity.class);
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Read from the database
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
String name = dataSnapshot.getKey();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(name);
builder.setContentText(value);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(value));
builder.setContentIntent(contentIntent);
builder.setSound(alarmSound);
builder.setAutoCancel(true);
notify=generateRandom();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notify, builder.build());
}
public int generateRandom(){
Random random = new Random();
return random.nextInt(9999 - 1000) + 1000;
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}