androidpush-notificationonesignal

How to disable OneSignal on android?


I want to use OneSignal on my app to send notifications to my users, and it is well documented and good, you should just act like this:

https://documentation.onesignal.com/docs/android-sdk-setup

OneSignal.startInit(this)
    .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
    .unsubscribeWhenNotificationsAreDisabled(true)
    .init();

The problem is, I want to just send notification to my logged in users, so if a user logged out, I dont want him to receive notification, how can I disable OneSignal after it's inited?

I don't want to handle this on server, it should be some way to stop OneSignal service, I think.


Solution

  • You can call the following method in the logout process.

    OneSignal.setSubscription(false);
    

    https://documentation.onesignal.com/docs/android-native-sdk#section--setsubscription-

    EDIT 2022:

    // RN SDK version 4.x.x

    OneSignal.disablePush(true);
    

    https://documentation.onesignal.com/docs/sdk-reference#disablepush-method