Ali Molaei
Ali Molaei

Reputation: 413

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.

Upvotes: 6

Views: 9041

Answers (4)

SOLUTION 2023: after the version [5.0.0, 5.99.99]


Disable Push:

OneSignal.User.pushSubscription.optOut()

Enable Push:

OneSignal.User.pushSubscription.optIn()

PS. Vel_daN: Love what You DO 💚.


Upvotes: 4

ko2ic
ko2ic

Reputation: 2092

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

Upvotes: 2

Devendra
Devendra

Reputation: 3444

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

Upvotes: 5

letsCode
letsCode

Reputation: 3046

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.

Upvotes: 3

Related Questions