为什么通知多次从Firebase中检索数据时被触发? [英] Why notifications are getting fired multiple times on retrieving data from Firebase?

查看:175
本文介绍了为什么通知多次从Firebase中检索数据时被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,当我按下它时,从 FirebaseDatabase 取得一些数据,然后一个通知被触发。
$ b $ p

以下是:

  aReference.child(rID)。 addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){$ b $ if(dataSnapshot.getValue()!= null){
Map< String,String> newRequest =(Map< String,String>)dataSnapshot.getValue();
uAU = newRequest.get(pName);

final int m = ((new Date().getTime()/ 1000L)%Integer.MAX_VALUE);

Intent notificationIntent = new Intent(getBaseContext(),NotificationARBroadcastReceiver.class);
notificationIntent.putExtra NotificationARBroadcastReceiver.NOTIFICATION,getNotificationAU());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),m,not ificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,0,pendingIntent);
}
}
});

以下是 NotificationARBroadcastReceiver.class

  public class NotificationARBroadcastReceiver extends BroadcastReceiver {

public static String NOTIFICATION =notification;
public static NotificationManager mNotifyMgr;
public static int m;
$ b @Override
public void onReceive(Context context,Intent intent){

m =(new Random())。nextInt(10000);
Log.d(mMain,String.valueOf(m));


mNotifyMgr =
(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);

通知通知= intent.getParcelableExtra(NOTIFICATION);
notification.defaults | = Notification.DEFAULT_SOUND;
notification.defaults | = Notification.DEFAULT_VIBRATE;
mNotifyMgr.cancel(m);
mNotifyMgr.notify(m,通知);




$ b $ p $ getNotificationAU()

  private通知getNotificationAU(){
mBuilder = new NotificationCompat。 Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle(title)
.setContentText(***);

意图resultIntent = new Intent(getBaseContext(),MainActivity.class);

PendingIntent resultPendingIntent = PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
返回mBuilder.build();

问题一旦通知被多次触发,许多相同的通知会显示在状态栏中。



为什么发生这种情况,以及如何只触发一次通知?



请让我知道。

解决方案

(code> mNotifyMgr.cancelAll(); 之前 mNotifyMgr.notify(m,通知); 为我完成了工作!


I have a button, when I press it some data is fetched from FirebaseDatabase and then a notification is fired.

Here's how:

aReference.child(rID).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.getValue() != null) {
            Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
            uAU = newRequest.get("pName");  

            final int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

            Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
            notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationAU());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);
        }
    }
});

Here's NotificationARBroadcastReceiver.class:

public class NotificationARBroadcastReceiver extends BroadcastReceiver {

    public static String NOTIFICATION = "notification";
    public static NotificationManager mNotifyMgr;
    public static int m;

    @Override
    public void onReceive(Context context, Intent intent) {

        m = (new Random()).nextInt(10000);
        Log.d("mMain", String.valueOf(m));


        mNotifyMgr =
                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotifyMgr.cancel(m);
        mNotifyMgr.notify(m, notification);

    }
}

Here's getNotificationAU():

private Notification getNotificationAU() {
    mBuilder = new NotificationCompat.Builder(getBaseContext())
                        .setSmallIcon(R.mipmap.app_icon_1)
                        .setContentTitle("title")
                        .setContentText(***);

    Intent resultIntent = new Intent(getBaseContext(), MainActivity.class);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(
                        getBaseContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(resultPendingIntent);
    return mBuilder.build();
}

The problem is that on pressing the button even once, notifications are fired multiple times and many same notifications are shown in the status bar.

Why is this happening and how to fire only one notification?

Please let me know.

解决方案

Adding mNotifyMgr.cancelAll(); (where mNotifyMgr is the NotificationManager) before mNotifyMgr.notify(m, notification); did the job for me!

这篇关于为什么通知多次从Firebase中检索数据时被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆