CMRyle
CMRyle

Reputation: 17

How to disable all the incoming push notification when your in the desired activity?

As the title implies, how can I prevent my app from getting notifications when I'm in the desired activity? I'm developing a chat application wherein users can get notifications when a new message has been posted, how can I prevent the notification when the user is in the chat activity?

here's FirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        super.onMessageReceived(message);

        int requestID = (int) System.currentTimeMillis();

        String title = message.getNotification().getTitle();
        String body = message.getNotification().getBody();
        String click_action=message.getNotification().getClickAction();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),"Notification");
        builder.setContentTitle(title);
        builder.setContentText(body);
        builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
        builder.setLights(getResources().getColor(R.color.chitchat), 3000, 3000);
        builder.setSmallIcon(R.drawable.logowhite);

        Intent intent = null;
        //message.getData().get("type");
        
        if (Objects.requireNonNull(message.getData().get("type")).equalsIgnoreCase("privatechat")) 
       {
            intent = new Intent(click_action);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK  | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("GCKey", message.getData().get("GCKey"));
            intent.putExtra("GCNameKey", message.getData().get("GCNameKey"));
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);
        
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("Notification", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(69, builder.build());
    }
}

Upvotes: 1

Views: 326

Answers (1)

Sambhav Khandelwal
Sambhav Khandelwal

Reputation: 3765

It should not be hard. You can just create a class where you have a static variable where you just set the activity and before notifying, check whether you want to show notification or not. This would go this way:

  1. Make a new class with a static variable

    public class NotificationHelper {
       public static boolean shouldShowNotification = true;
    }
    
  2. In the activity you don't want the notification to show in add this code:

    @Override
    public void onResume(){
       super.onResume();
       NotificationHelper.shouldShowNotification = false; 
    }
    
    @Override
    public void onPause(){
       super.onPause();
       NotificationHelper.shouldShowNotification = true; 
    }
    
  3. In the MyFirebaseMessagingService class, add a condition to before executing the code.

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
     @Override
     public void onMessageReceived(@NonNull RemoteMessage message) {
           super.onMessageReceived(message);
           if(NotificationHelper.shouldShowNotification){
              // your code goes here...
           }
        }
    }
    

Upvotes: 0

Related Questions