Package 

Object OneSignal


  • 
    public class OneSignal
    
                        

    This 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".

    • 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 operations
        appId - 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.
      • logout

         final static Unit logout()

        Logout the user previously logged in via login. 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.

      • 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.

      • 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.