-
public class OneSignalThis singleton class is the entry point to the OneSignal SDK. It is designed to make OneSignal easy to use.
No instance management is required from the app developer.
This is a wrapper around an instance of IOneSignal, no logic lives in this class.
Note: This does not implement IOneSignal itself because you cannot specify @JvmStatic on an override function. The cleanliness that @JvmStatic gives when calling from java outweighs the use of an interface. This class however should implement IOneSignal "in spirit".
-
-
Field Summary
Fields Modifier and Type Field Description private final static BooleanisInitializedprivate final static StringsdkVersionprivate final static IUserManagerUserprivate final static ISessionManagerSessionprivate final static INotificationsManagerNotificationsprivate final static ILocationManagerLocationprivate final static IInAppMessagesManagerInAppMessagesprivate final static IDebugManagerDebugprivate static BooleanconsentRequiredprivate static BooleanconsentGivenprivate static BooleandisableGMSMissingPromptprivate final IServiceProviderservicespublic final static OneSignalINSTANCE
-
Method Summary
Modifier and Type Method Description final static UnitinitWithContext(Context context, String appId)Initialize the OneSignal SDK. final static BooleaninitWithContext(Context context)Used to initialize the SDK when driven through user action. final static BooleaninitWithContextSuspend(Context context, String appId)Initialize the OneSignal SDK asynchronously. final static IUserManagergetUserSuspend()Get the user manager without blocking the calling thread. final static ISessionManagergetSessionSuspend()Get the session manager without blocking the calling thread. final static INotificationsManagergetNotificationsSuspend()Get the notifications manager without blocking the calling thread. final static ILocationManagergetLocationSuspend()Get the location manager without blocking the calling thread. final static IInAppMessagesManagergetInAppMessagesSuspend()Get the in-app messages manager without blocking the calling thread. final static BooleangetConsentRequiredSuspend()Get the consent required flag in a thread-safe manner without blocking the calling thread. final static UnitsetConsentRequiredSuspend(Boolean required)Set the consent required flag in a thread-safe manner without blocking the calling thread. final static BooleangetConsentGivenSuspend()Get the consent given flag in a thread-safe manner without blocking the calling thread. final static UnitsetConsentGivenSuspend(Boolean value)Set the consent given flag in a thread-safe manner without blocking the calling thread. final static BooleangetDisableGMSMissingPromptSuspend()Get the disable GMS missing prompt flag in a thread-safe manner without blocking the calling thread. final static UnitsetDisableGMSMissingPromptSuspend(Boolean value)Set the disable GMS missing prompt flag in a thread-safe manner without blocking the calling thread. final static Unitlogin(String externalId)Login to OneSignal under the user identified by the externalId provided. final static Unitlogin(String externalId, String jwtBearerToken)Login to OneSignal under the user identified by the externalId provided. final static Unitlogout()Logout the user previously logged in via login. final static UnitupdateUserJwt(String externalId, String token)Update the JWT bearer token associated with externalId. final static UnitaddUserJwtInvalidatedListener(IUserJwtInvalidatedListener listener)Subscribe a listener for JWT-invalidated events. final static UnitremoveUserJwtInvalidatedListener(IUserJwtInvalidatedListener listener)Unsubscribe a listener previously registered via addUserJwtInvalidatedListener. final static UnitloginSuspend(String externalId, String jwtBearerToken)Login a user with external ID and optional JWT token without blocking the calling thread. final static UnitlogoutSuspend()Logout the current user without blocking the calling thread. final static UnitupdateUserJwtSuspend(String externalId, String token)Update the JWT bearer token associated with externalId without blocking the calling thread. final <T extends Any> TgetService()Inline function to retrieve a specific service. final <T extends Any> TgetServiceOrNull()Inline function to retrieve a specific service, or null if that service does not exist. final static BooleangetIsInitialized()final static StringgetSdkVersion()final static IUserManagergetUser()final static ISessionManagergetSession()final static INotificationsManagergetNotifications()final static ILocationManagergetLocation()final static IInAppMessagesManagergetInAppMessages()final static IDebugManagergetDebug()final static BooleangetConsentRequired()final static UnitsetConsentRequired(Boolean consentRequired)final static BooleangetConsentGiven()final static UnitsetConsentGiven(Boolean consentGiven)final static BooleangetDisableGMSMissingPrompt()final static UnitsetDisableGMSMissingPrompt(Boolean disableGMSMissingPrompt)final IServiceProvidergetServices()-
-
Method Detail
-
initWithContext
final static Unit initWithContext(Context context, String appId)
Initialize the OneSignal SDK. This should be called during startup of the application.
- Parameters:
context- The Android context the SDK should use.appId- The application ID the OneSignal SDK is bound to.
-
initWithContext
final static Boolean initWithContext(Context context)
Used to initialize the SDK when driven through user action. It is assumed initWithContext has been called by the app developer, providing the appId, which has been cached for this purpose.
THIS IS AN INTERNAL INTERFACE AND SHOULD NOT BE USED DIRECTLY.
-
initWithContextSuspend
final static Boolean initWithContextSuspend(Context context, String appId)
Initialize the OneSignal SDK asynchronously. This should be called during startup of the application. This method provides a suspended version that returns a boolean indicating success. Uses Dispatchers.IO internally to prevent ANRs and optimize for I/O operations.
- Parameters:
context- Application context is recommended for SDK operationsappId- The application ID the OneSignal SDK is bound to.
-
getUserSuspend
final static IUserManager getUserSuspend()
Get the user manager without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the User property accessor.
-
getSessionSuspend
final static ISessionManager getSessionSuspend()
Get the session manager without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the Session property accessor.
-
getNotificationsSuspend
final static INotificationsManager getNotificationsSuspend()
Get the notifications manager without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the Notifications property accessor.
-
getLocationSuspend
final static ILocationManager getLocationSuspend()
Get the location manager without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the Location property accessor.
-
getInAppMessagesSuspend
final static IInAppMessagesManager getInAppMessagesSuspend()
Get the in-app messages manager without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the InAppMessages property accessor.
-
getConsentRequiredSuspend
final static Boolean getConsentRequiredSuspend()
Get the consent required flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the consentRequired property accessor.
-
setConsentRequiredSuspend
final static Unit setConsentRequiredSuspend(Boolean required)
Set the consent required flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the consentRequired property setter.
- Parameters:
required- Whether a user must consent to privacy prior to their user data being sent to OneSignal.
-
getConsentGivenSuspend
final static Boolean getConsentGivenSuspend()
Get the consent given flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the consentGiven property accessor.
-
setConsentGivenSuspend
final static Unit setConsentGivenSuspend(Boolean value)
Set the consent given flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the consentGiven property setter.
- Parameters:
value- Whether privacy consent has been granted.
-
getDisableGMSMissingPromptSuspend
final static Boolean getDisableGMSMissingPromptSuspend()
Get the disable GMS missing prompt flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the disableGMSMissingPrompt property accessor.
-
setDisableGMSMissingPromptSuspend
final static Unit setDisableGMSMissingPromptSuspend(Boolean value)
Set the disable GMS missing prompt flag in a thread-safe manner without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the disableGMSMissingPrompt property setter.
- Parameters:
value- Whether to disable the "GMS is missing" prompt to the user.
-
login
final static Unit login(String externalId)
Login to OneSignal under the user identified by the externalId provided. The act of logging a user into the OneSignal SDK will switch the User context to that specific user.
If the externalId exists the user will be retrieved and the context set from that user information. If operations have already been performed under a guest user, they will not be applied to the now logged in user (they will be lost).
If the externalId does not exist the user will be created and the context set from the current local state. If operations have already been performed under a guest user those operations will be applied to the newly created user.
Push Notifications and In App Messaging Logging in a new user will automatically transfer push notification and in app messaging subscriptions from the current user (if there is one) to the newly logged in user. This is because both Push and IAM are owned by the device.
- Parameters:
externalId- The external ID of the user that is to be logged in.
-
login
final static Unit login(String externalId, String jwtBearerToken)
Login to OneSignal under the user identified by the externalId provided. The act of logging a user into the OneSignal SDK will switch the User context to that specific user.
If the externalId exists the user will be retrieved and the context set from that user information. If operations have already been performed under a guest user, they will not be applied to the now logged in user (they will be lost).
If the externalId does not exist the user will be created and the context set from the current local state. If operations have already been performed under a guest user those operations will be applied to the newly created user.
Push Notifications and In App Messaging Logging in a new user will automatically transfer push notification and in app messaging subscriptions from the current user (if there is one) to the newly logged in user. This is because both Push and IAM are owned by the device.
- Parameters:
externalId- The external ID of the user that is to be logged in.jwtBearerToken- The optional JWT bearer token generated by your backend to establish trust for the login operation.
-
updateUserJwt
final static Unit updateUserJwt(String externalId, String token)
Update the JWT bearer token associated with externalId. Use this when your backend has issued a new JWT for an already-logged-in user (e.g. in response to a previous IUserJwtInvalidatedListener.onUserJwtInvalidated callback). Stores the JWT and wakes the operation queue so any deferred ops can dispatch with the fresh token.
- Parameters:
externalId- The external ID the JWT belongs to.token- The new JWT bearer token issued by your backend.
-
addUserJwtInvalidatedListener
final static Unit addUserJwtInvalidatedListener(IUserJwtInvalidatedListener listener)
Subscribe a listener for JWT-invalidated events. Fires on a background thread when the SDK detects that the stored JWT for a user is no longer valid (typically after a 401 from the OneSignal backend). Apps should respond by fetching a fresh JWT from their backend and supplying it via updateUserJwt.
Pure pub/sub: only listeners subscribed at the time of the invalidation receive the event. Subscribe early (e.g. in
Application.onCreate) to avoid missing events.
-
removeUserJwtInvalidatedListener
final static Unit removeUserJwtInvalidatedListener(IUserJwtInvalidatedListener listener)
Unsubscribe a listener previously registered via addUserJwtInvalidatedListener.
-
loginSuspend
final static Unit loginSuspend(String externalId, String jwtBearerToken)
Login a user with external ID and optional JWT token without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the login method.
The act of logging a user into the OneSignal SDK will switch the User context to that specific user.
If the externalId exists the user will be retrieved and the context set from that user information. If operations have already been performed under a guest user, they will not be applied to the now logged in user (they will be lost).
If the externalId does not exist the user will be created and the context set from the current local state. If operations have already been performed under a guest user those operations will be applied to the newly created user.
Push Notifications and In App Messaging Logging in a new user will automatically transfer push notification and in app messaging subscriptions from the current user (if there is one) to the newly logged in user. This is because both Push and IAM are owned by the device.
- Parameters:
externalId- The external ID of the user that is to be logged in.jwtBearerToken- The optional JWT bearer token generated by your backend to establish trust for the login operation.
-
logoutSuspend
final static Unit logoutSuspend()
Logout the current user without blocking the calling thread. Suspends until the SDK is initialized if initialization is in progress. This is the suspend-safe version of the logout method.
The User property now references a new device-scoped user. A device-scoped user has no user identity that can later be retrieved, except through this device as long as the app remains installed and the app data is not cleared.
-
updateUserJwtSuspend
final static Unit updateUserJwtSuspend(String externalId, String token)
Update the JWT bearer token associated with externalId without blocking the calling thread. Suspend-safe version of updateUserJwt.
- Parameters:
externalId- The external ID the JWT belongs to.token- The new JWT bearer token issued by your backend.
-
getService
final <T extends Any> T getService()
Inline function to retrieve a specific service.
THIS IS AN INTERNAL INTERFACE AND SHOULD NOT BE USED DIRECTLY.
-
getServiceOrNull
final <T extends Any> T getServiceOrNull()
Inline function to retrieve a specific service, or null if that service does not exist.
THIS IS AN INTERNAL INTERFACE AND SHOULD NOT BE USED DIRECTLY.
-
getIsInitialized
final static Boolean getIsInitialized()
-
getSdkVersion
final static String getSdkVersion()
-
getUser
final static IUserManager getUser()
-
getSession
final static ISessionManager getSession()
-
getNotifications
final static INotificationsManager getNotifications()
-
getLocation
final static ILocationManager getLocation()
-
getInAppMessages
final static IInAppMessagesManager getInAppMessages()
-
getDebug
final static IDebugManager getDebug()
-
getConsentRequired
final static Boolean getConsentRequired()
-
setConsentRequired
final static Unit setConsentRequired(Boolean consentRequired)
-
getConsentGiven
final static Boolean getConsentGiven()
-
setConsentGiven
final static Unit setConsentGiven(Boolean consentGiven)
-
getDisableGMSMissingPrompt
final static Boolean getDisableGMSMissingPrompt()
-
setDisableGMSMissingPrompt
final static Unit setDisableGMSMissingPrompt(Boolean disableGMSMissingPrompt)
-
getServices
final IServiceProvider getServices()
-
-
-
-