CompEng
CompEng

Reputation: 7386

Android How can I hide notification alert when the app is closed?

I implement the onesignal app on android. It works fine. But I want to send notification from web to all users with custom data and I want to users dont see any notification on bar. Of course I do something in the background when notificatin comes.

How can I do that?

thanks in advance

my code:

OneSignal.startInit(this)
        .setNotificationReceivedHandler(new NotificationReceivedHandler())
        .setNotificationOpenedHandler(new NotificationOpenedHandler())
        .init();

OneSignal.setSubscription(true);

I do something here

class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {
            JSONObject data = notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("custom", null);
                if (customKey != null)
                {
                    //I do sth here
                }
            }
        }
    }

Upvotes: 1

Views: 821

Answers (2)

ViramP
ViramP

Reputation: 1709

Can you once check with below code snippet ?

 class NotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
        @Override
        public void notificationReceived(OSNotification notification) {
            JSONObject data = notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("custom", null);
                if (customKey != null)
                {
                    //I do sth here
                    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    nMgr.cancelAll();
                }
            }
        }
    }

If still facing issue post issue here : https://github.com/OneSignal/OneSignal-Android-SDK/issues

Upvotes: 3

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4930

call this metod on your Activity's onBackPressed() metod

public void cancelNotification() {

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) YourActivity.this.getSystemService(ns);
    nMgr.cancel(1);
}

Upvotes: 1

Related Questions