urbanairship / android-library

Urban Airship Android SDK
Other
109 stars 123 forks source link

Urban Airship not showing push notifications while app foreground #216

Closed worm69 closed 2 years ago

worm69 commented 2 years ago

Hi, how i can show push notifications while app is on foreground? iOS have PushNotificationDelegate and on Android how i can do that?

rlepinski commented 2 years ago

Android shows notifications in foreground by default. You have to do extra dev work to disable that at the moment.

worm69 commented 2 years ago

@rlepinski thx for quick reply. For now i will drop that feature for now but any tips how i can do that on Android?

rlepinski commented 2 years ago

@worm69 What is your goal? Making sure they display in the foreground or not?

worm69 commented 2 years ago

my goal is be able to show them on foreground if i need

rlepinski commented 2 years ago

Here is a custom notification provider that you can use to suppress notifications in the foreground (default is to show them), but you can add additional flags if you want:

class CustomNotificationProvider extends AirshipNotificationProvider {

    public CustomNotificationProvider(@NonNull Context context, @NonNull AirshipConfigOptions configOptions) {
        super(context, configOptions);
    }

    @NonNull
    @Override
    public NotificationResult onCreateNotification(@NonNull Context context, @NonNull NotificationArguments arguments) {
        if (!shouldDisplay(context, arguments)) {
            return NotificationResult.cancel();
        }
        return super.onCreateNotification(context, arguments);
    }

    private boolean shouldDisplay(@NonNull Context context, @NonNull NotificationArguments arguments) {
        if (GlobalActivityMonitor.shared(context).isAppForegrounded()) {
            // Do whatever you want here
            return false;
        } else {
            return true;
        }
    }
}

Set the provider in the Autopilot#onAirshipReady callback:

CustomNotificationProvider provider = new CustomNotificationProvider(UAirship.getApplicationContext(), airship.getAirshipConfigOptions());
airship.getPushManager().setNotificationProvider(provider);

Hopefully in a future release you can just say UAirship.shared().getPushManager().isForegroundNotificationDisplayEnabled(false); or something of those sorts

worm69 commented 2 years ago

Many thanks @rlepinski have a good weekend