为什么我仅在打开活动时收到通知? [英] Why I get the Notification just opening the Activity?
问题描述
有人在STACKOVERFLOW上对此有解决方案吗?
Edit : DO anyone have a solution for this on STACKOVERFLOW ?
我正在使用Firebase数据库,并希望在添加新子项后立即显示通知.
I am using Firebase Database and wanted to show notification as soon as new child is added.
我正在使用"ChildEventListener"来获取它.
I am using "ChildEventListener" to acquire it.
这是我的代码:
dbProducts.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
// Toast.makeText(ShowNotifActivity.this, "Child Added ", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ShowNotifActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "MyNotifications";
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId)
.setSmallIcon(R.drawable.notification)
// .setLargeIcon(R.drawable.ic_notifications_active_black_24dp)
.setContentTitle("New Notification")
.setContentText("Click here to View ");
;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
// Toast.makeText(ShowNotifActivity.this, "Child Changed", Toast.LENGTH_SHORT).show();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
在Firebase数据库中添加新子项后,如何仅显示一次通知.
How can the Notification be shown only once as a new child is added in firebase database.
问题是,一旦我打开Activity,通知就会触发,即使我没有更新它也没有更改或添加数据库中的任何内容.怎么解决
the problem is as soon as i open the Activity , the notification triggers even when i have not updated it changed or added anything in the database. How can it be fixed
推荐答案
当您使用addChildEventListener
在数据库中的某个位置附加一个侦听器时,即使该位置上的每个孩子都被调用一次onChildAdded
,即使这不是新的.这就是为什么您的通知始终显示的原因.
When you use addChildEventListener
to attach a listener at a location in the database, onChildAdded
will get called once for each child at that location, even if it isn't new. That's why your notification is always showing.
如果您只想获取新"项目,则需要想出一种方法来确定用户实际上是哪些新孩子,并为查询中的那些孩子进行过滤.也许时间戳或其他标记会为您工作.
If you only want to get "new" items, you will need to come up with a way to determine which children are actually new to the user, and filter for those children in your query. Maybe a timestamp or some other flag will work for you.
这篇关于为什么我仅在打开活动时收到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!