0

버튼이 있습니다.이 버튼을 누르면 데이터가 FirebaseDatabase에서 가져온 다음 notification이 해고됩니다.Firebase에서 데이터를 검색 할 때 알림이 여러 번 열리는 이유는 무엇입니까?

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

여기 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); 

    } 
} 

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

문제도 한 번 버튼을 누르면에 알림 해고된다는 것입니다 여러 번 많은 동일한 알림이 상태 표시 줄에 표시됩니다.

일까요? 하나의 알림 만 실행하는 이유는 무엇입니까?

알려 주시기 바랍니다.

+0

'aReference.child (rID) '아래에 여러 개의 자식이있는 것처럼 보입니다.'onChildAdded'가 호출 될 때마다 새로운 알림이 표시됩니다. – Wilik

+0

@Wilik 예 여러 자식이 있습니다. 그렇다면이 상황에서도 알림을 한 번만 표시하려면 어떻게해야합니까? –

+0

알림 목적에 따라 ValueEventListener를 대신 사용하거나 모든 알림에 대해 동일한 ID를 설정하거나 새 알림을 시작하기 전에 알림이 표시되는지 확인할 수 있습니다. – Wilik

답변

0

(여기에서 mNotifyMgrNotificationManager)을 추가하기 전에 mNotifyMgr.notify(m, notification);이 나를 위해 일했습니다!