0

here is my code the problem is it gets triggered every time when the data gets load what i want is method should be called only when the database changes and not when the data is loaded in the app

   FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference reference = firebaseDatabase.getReference();

reference.child("Requests").addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    DisplayNotification(getContext(),"New Order has been received");
  }

  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {

  }
});


 public void DisplayNotification(Context context, String message) {
    // Create an explicit intent for an Activity in your app
    Intent intent = new Intent(getContext(), MainActivity_User.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Uri NOTIFICATION_SOUND_URI = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.fillingyourinbox);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "Default")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("New Order")
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(1, mBuilder.build());
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Check **[this](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)** out. – Alex Mamo Nov 15 '19 at 10:38

2 Answers2

2

Can you just ignore first call in onDataChange?

private DataSnapshot lastObtainedDataSnapshot;
@Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (lastObtainedDataSnapshot == null) {
    DisplayNotification(getContext(),"New Order has been received");
    }
    lastObtainedDataSnapshot = dataSnapshot;
  }
1

You need to use cloud functions to achieve the requirement.

With Cloud Functions, you can handle events in the Firebase Realtime Database with no need to update client code. Cloud Functions lets you run database operations with full administrative privileges, and ensures that each change to the database is processed individually.

onWrite(), which triggers when data is created, updated, or deleted in the Realtime Database.

onCreate(), which triggers when new data is created in the Realtime Database.

onUpdate(), which triggers when data is updated in the Realtime Database.

onDelete(), which triggers when data is deleted from the Realtime Database

In your case you need to use onCreate() database trigger. You can read more about cloud function here:

https://firebase.google.com/docs/functions/database-events

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134