As title what I want to do is only when database had change than run notification for it but a problem is when I run app the notification appear even the data is not change here is my code:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.main,container,false);
result= (TextView) v.findViewById(R.id.textView3);
notificationmanager = NotificationManagerCompat.from(ct.getcontext());
if (user != null) {
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String post = dataSnapshot.getValue(String.class);
result.setText(post);
Notification notification = new NotificationCompat.Builder(ct.getcontext(), CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(post)
.setContentText("test")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_EVENT)
.setDefaults(Notification.DEFAULT_ALL)
.build();
notificationmanager.notify(2,notification);
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
else
{
Toast.makeText(getActivity(),"not yet login",Toast.LENGTH_LONG).show();
}
return v;
}