'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 1:[1]

You can call the following method in the logout process.

OneSignal.setSubscription(false);

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

Solution 2:[2]

OneSignal has something called segments... (flags).

You can set a flag with OneSignal. If the user is logged in, they belong to a flag that logged out users don't.

For example, if logged in, set a flag for OneSignal as loggedin. If logged out, set the flag for OneSignal for this user as loggedout. When you send out notifications, you only send it to the loggedin flag.

Solution 3:[3]

We can easily enable/disable the OneSignal push notification using the below method:

OneSignal.disablePush(boolean)

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ko2ic
Solution 2 letsCode
Solution 3