【发布时间】:2019-11-15 10:18:16
【问题描述】:
这是我的代码 问题是每次加载数据时都会触发它 我想要的是仅在数据库更改而不是在应用程序中加载数据时才应调用方法
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());
}
【问题讨论】:
-
签出this。
标签: android firebase firebase-realtime-database notifications