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());
}
}
CodePudding user response:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.get(0).topActivity.getClassName().equals(MyDesiredActivity.class.getName())) { // Show the notification } In the code above, we first get the ActivityManager and use it to get a list of the currently running tasks. Then, we check the top activity in the list of tasks and compare it to the name of the desired activity. If the top activity is not the desired activity, then we show the notification.
The code above uses the ActivityManager which is deprecated in Android 10 (API level 29) and higher. in newer versions of Android, you should use the getForegroundActivity() method of the ActivityTaskManager class instead Hope this helps xx
CodePudding user response:
Well to prevent your app from displaying notifications when the user is in the chat activity, you can check if the chat activity is currently in the foreground before showing the notification. This can be done by checking the "Importance" of the app's current process, which can be obtained using the ActivityManager class. If the app's process is considered "Important" or "Foreground", then the notification should not be shown.
Here is an example of how this can be implemented in the onMessageReceived() method of your MyFirebaseMessagingService class:
@Override
public void onMessageReceived(@NonNull RemoteMessage message) { super.onMessageReceived(message);
// Check if the app's process is considered "Important" or "Foreground"
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
int importance = runningAppProcesses.get(0).importance;
boolean isForeground = (importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ||
importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE ||
importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING);
// If the app's process is not considered "Important" or "Foreground", show the notification
if (!isForeground) {
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;
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);
I hope this helps xxx