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());
}