0

I'm using pushy.me push notification service in my android app. It works fine, but I only want to show the push notification when this app is NOT running in foreground.

This are the lines where I build the notification:

        // Prepare a notification with vibration, sound and lights
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setAutoCancel(true)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setLights(Color.RED, 1000, 1000)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyApp.class), PendingIntent.FLAG_UPDATE_CURRENT));

        // Automatically configure a Notification Channel for devices running Android O+
        Pushy.setNotificationChannel(builder, context);

        // Get an instance of the NotificationManager service
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        // Build the notification and display it

        notificationManager.notify(1, builder.build());

How can I check if the app is running in foreground?

Thanks!

simonheinrich
  • 91
  • 1
  • 8

1 Answers1

0

You can achieve this at the activity level by having a flag that you set to true in onResume and set to false in onPause. It depends on the particular architecture of your application, but there is no way to know if an App is in the foreground, only if an Activity is.

Benjamin Cassidy
  • 827
  • 1
  • 8
  • 23