0

Ich habe ein Problem, brauche deine Hilfe, ich habe meine App im Vordergrund und wenn ich die Benachrichtigung vom Server senden, dann kommen die Benachrichtigung mit dem Benachrichtigungssymbol.Wie kann ich das Benachrichtigungssymbol schließen, wenn sich meine App im Vordergrund befindet?

aber was ich brauche, wenn meine App im Vordergrund Benutzer ist, sieht die Benachrichtigung dann Benachrichtigung Symbol sollte nicht für den Benutzer angezeigt werden.

Wenn meine App im Hintergrund ist/oder wenn die App nicht gestartet wird, sollte die Benachrichtigung mit dem Benachrichtigungssymbol erscheinen und das Symbol für die Benutzerbenachrichtigung und das Benachrichtigungssymbol werden ignoriert.

Ich hatte versucht:

if(getIntent()!=null){ 
     Log.e("getIntent","getIntent"); 

     broadcastReciver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       if (intent.getAction().equals(NOTIFY_ACTIVITY_ACTION)) 
       { 
        if(intent.getExtras().getBoolean("reload")){ 

         int notificationId = intent.getIntExtra("notificationId", 0); 
         Log.e("notificationId","notificationId---"+notificationId); 
         NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
         manager.cancel(notificationId); 

         setFragmentView(0); 

         //finish(); 
        } 
       } 
      } 
     }; 
    } 

In onMessageReceived:

Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra("notificationId",NOTIFICATION_ID); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.splash_logo) 
       .setContentTitle("Logizo") 
       .setContentText("New Order Arrived") 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(NOTIFICATION_ID/* ID of notification */, notificationBuilder.build()); 
+0

was genau möchten Sie tun? Sie möchten nicht, dass die Benachrichtigung angezeigt wird, wenn sich Ihre App im Vordergrund befindet? – Prashant

+0

@ Prashant ja es ist jetzt gelöst. –

Antwort

0

I Benachrichtigungen Symbole entfernen, indem Sie folgenden Code verwenden.

private void removeNotificationsFromStatusBar(Context context, int notificationId) { 

    try { 
     NotificationManager notificationManagerNS = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManagerNS.cancel(notificationId); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.e(NotificationsFragment.class.getSimpleName(), "Unable to remove notification from status bar"); 
    } 

} 
+0

wo muss ich diesen Code setzen? in der Launcher-Aktivität oder Benachrichtigungsdienst? –

+0

Sie können diesen Code überall verwenden, müssen jedoch einen Kontext haben und mithilfe von Context Manager Notification Manager erstellen und die Benachrichtigung dann mit notificationId abbrechen. –

+0

Überprüfen Sie den aktualisierten Code. –

Verwandte Themen