Package 

Object UnifiedPush


  • 
    public class UnifiedPush
    
                        

    Object containing functions to interact with the distributor

    <!-- Note: This must be mirrored in Module.md -->

    Users are allowed to define a default distributor on their system, because UnifiedPush distributors have to be able to process a deeplink.

    When you set UnifiedPush for the first time on your application, you will want to use the default user's distributor.

    From time to time, like every time you starts your application, you should register your application in case the user have uninstalled the previous distributor. If the previous distributor is uninstalled, you can fallback to the default one again.

    Therefore, you can use tryUseCurrentOrDefaultDistributor to select the saved distributor or the default one when your application starts (when your main activity is created for instance).

    When the distributor is saved, you can call register to request a new registration. It has optional parameters, the following example uses messageForDistributor and vapid. You can use instance to bring multiple-registration support to your application.

    If you want, you can use the library org.unifiedpush.android:connector-ui instead, it displays a dialog explaining why the OS picker is going to ask which application to pick.

    <div class="tabs"> <input class="tabs_control hidden" type="radio" id="tabs-trydefault-receiver-0" name="tabs-trydefault" checked> <label class="tabs_label" for="tabs-trydefault-receiver-0">Kotlin</label> <div class="tabs_content"> <!-- CONTENT KOTLIN -->
    import org.unifiedpush.android.connector.UnifiedPush
    /* ... */
    
    UnifiedPush.tryUseCurrentOrDefaultDistributor(context) { success ->
        if (success) {
            // We have a distributor
            // Register your app to the distributor
            UnifiedPush.register(context, messageForDistributor, vapid)
        }
    }
    <!-- END KOTLIN --></div> <input class="tabs_control hidden" type="radio" id="tabs-trydefault-receiver-1" name="tabs-trydefault"> <label class="tabs_label" for="tabs-trydefault-receiver-1">Java</label> <div class="tabs_content"> <!-- CONTENT JAVA -->
    import static org.unifiedpush.android.connector.ConstantsKt.INSTANCE_DEFAULT;
    import org.unifiedpush.android.connector.UnifiedPush;
    /* ... */
    
    UnifiedPush.tryUseCurrentOrDefaultDistributor(context, success ->{
        if (success) {
            // We have a distributor
            // Register your app to the distributor
            UnifiedPush.register(
                context,
                INSTANCE_DEFAULT,
                messageForDistributor,
                vapid
            );
        }
    });
    <!-- END JAVA --></div> </div>

    Be aware that tryUseDefaultDistributor starts a new translucent activity in order to get the result of the distributor activity. You may prefer to use LinkActivityHelper directly in your own activity instead.

    You will probably want to allow the users to use another distributor but their default one.

    For this, you can get the list of available distributors with getDistributors.

    Once the user has chosen the distributor, you have to save it with saveDistributor. This function must be called before register.

    When the distributor is saved, you can call register to request a new registration. It has optional parameters, the following example uses messageForDistributor and vapid. You can use instance to bring multiple-registration support to your application.

    If you want, the library org.unifiedpush.android:connector-ui offers a customizable dialog that request user's choice and register to this distributor.

    <div class="tabs"> <input class="tabs_control hidden" type="radio" id="tabs-1-receiver-0" name="tabs-1" checked> <label class="tabs_label" for="tabs-1-receiver-0">Kotlin</label> <div class="tabs_content"> <!-- CONTENT KOTLIN -->
    import org.unifiedpush.android.connector.UnifiedPush
    /* ... */
    
    // Get a list of distributors that are available
    val distributors = UnifiedPush.getDistributors(context)
    // select one or ask the user which distributor to use, eg. with a dialog
    val userDistrib = yourFunc(distributors)
    // save the distributor
    UnifiedPush.saveDistributor(context, userDistrib)
    // register your app to the distributor
    UnifiedPush.register(context, messageForDistributor, vapid)
    <!-- END KOTLIN --></div> <input class="tabs_control hidden" type="radio" id="tabs-1-receiver-1" name="tabs-1"> <label class="tabs_label" for="tabs-1-receiver-1">Java</label> <div class="tabs_content"> <!-- CONTENT JAVA -->
    import static org.unifiedpush.android.connector.ConstantsKt.INSTANCE_DEFAULT;
    import org.unifiedpush.android.connector.UnifiedPush;
    /* ... */
    
    // Get a list of distributors that are available
    List<String> distributors = UnifiedPush.getDistributors(context);
    // select one or show a dialog or whatever
    String userDistrib = yourFunc(distributors);
    // the below line will crash the app if no distributors are available
    UnifiedPush.saveDistributor(context, userDistrib);
    UnifiedPush.register(
        context,
        INSTANCE_DEFAULT,
        messageForDistributor,
        vapid
    );
    <!-- END JAVA --></div> </div>

    To unsubscribe, simply call unregister. Set the instance you want to unsubscribed to if you used one during registration.

    It removes the distributor if this is the last instance to unregister.

    • Method Detail

      • register

         final static Unit register(Context context, String instance, String messageForDistributor, String vapid)

        Request a new registration for the instance to the saved distributor.

        saveDistributor must be called before this function.

        If there was a distributor but it has been removed, MessagingReceiver.onUnregistered will be called for all subscribed instances.

        Parameters:
        context - To interact with the shared preferences and send broadcast intents.
        instance - Registration instance.
        messageForDistributor - May be shown by the distributor UI to identify this registration.
        vapid - VAPID public key (RFC8292) base64url encoded of the uncompressed form (87 chars long).
      • unregister

         final static Unit unregister(Context context, String instance)

        Send an unregistration request for the instance to the saved distributor and remove the registration. Remove the distributor if this is the last instance registered.

        MessagingReceiver.onUnregistered won't be called after that request.

        Parameters:
        context - To interact with the shared preferences and send broadcast intents.
        instance - Registration instance.
      • tryUseDefaultDistributor

         final static Unit tryUseDefaultDistributor(Context context, Function1<Boolean, Unit> callback)

        Try to use the distributor opening the deeplink "unifiedpush://link"

        It allows users to define a default distributor for all their applications

        External distributors will be favored over embedded distributors.

        Be aware that this function starts a new translucent activity in order to get the result of the distributor activity. You may prefer to use LinkActivityHelper directly in your own activity instead.

        Kotlin:

        tryUseDefaultDistributor(context) { success ->
            if (success) {
                //TODO: register
            }
        }

        Java:

        UnifiedPush.tryUseDefaultDistributor(context, success -> {
            if (success) {
                //TODO: register
            }
            return null;
        });
        Parameters:
        context - Must be an activity or it will fail and the callback will be called with false
        callback - is a function taking a Boolean as parameter.
      • tryUseCurrentOrDefaultDistributor

         final static Unit tryUseCurrentOrDefaultDistributor(Context context, Function1<Boolean, Unit> callback)

        Try to use the saved distributor else, use the default distributor opening the deeplink "unifiedpush://link"

        It can be used on application startup to register to the distributor. If you had already registered to a distributor, this ensure the connection is working. If the previous distributor has been uninstalled, it will fallback to the user's default. If you register for the first time, it will use the user's default Distributor or the OS will ask what it should use.

        External distributors will be favored over embedded distributors.

        Be aware that this function may start a new translucent activity in order to get the result of the distributor activity. You may prefer to use LinkActivityHelper directly in your own activity instead.

        Kotlin:

        tryUseCurrentOrDefaultDistributor(context) { success ->
            if (success) {
                //TODO: register
            }
        }

        Java:

        UnifiedPush.tryUseCurrentOrDefaultDistributor(context, success -> {
            if (success) {
                //TODO: register
            }
            return null;
        });
        Parameters:
        context - Must be an activity or it will fail if there is no current distributor and the callback will be called with false
        callback - is a function taking a Boolean as parameter.
      • saveDistributor

         final static Unit saveDistributor(Context context, String distributor)

        Save distributor as the new distributor to use

        Parameters:
        context - To interact with the shared preferences.
        distributor - The distributor package name
      • removeDistributor

         final static Unit removeDistributor(Context context)

        Unregister all instances and remove the distributor

        Parameters:
        context - To interact with the shared preferences and send broadcast intents.