-
public class UnifiedPushObject 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
registerto request a new registration. It has optional parameters, the following example usesmessageForDistributorandvapid. You can useinstanceto bring multiple-registration support to your application.If you want, you can use the library
<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 -->org.unifiedpush.android:connector-uiinstead, it displays a dialog explaining why the OS picker is going to ask which application to pick.
<!-- 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 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 JAVA --></div> </div>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 ); } });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 beforeregister.When the distributor is saved, you can call
registerto request a new registration. It has optional parameters, the following example usesmessageForDistributorandvapid. You can useinstanceto bring multiple-registration support to your application.If you want, the library
<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 -->org.unifiedpush.android:connector-uioffers a customizable dialog that request user's choice and register to this distributor.
<!-- 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 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 JAVA --></div> </div>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 );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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classUnifiedPush.VapidNotValidExceptionThe VAPID public key is not in the right format.
It should be in the uncompressed form, and base64url encoded (87 chars long)
-
Field Summary
Fields Modifier and Type Field Description public final static UnifiedPushINSTANCE
-
Method Summary
Modifier and Type Method Description final static Unitregister(Context context, String instance, String messageForDistributor, String vapid)Request a new registration for the instance to the saved distributor. final static Unitregister(Context context, String instance, String messageForDistributor, String vapid, KeyManager keyManager)register with additional KeyManager parameter. final static UnitregisterApp(Context context, String instance, String messageForDistributor, String vapid)final static Unitunregister(Context context, String instance)Send an unregistration request for the instance to the saved distributor and remove the registration. final static Unitunregister(Context context, String instance, KeyManager keyManager)unregister with additional KeyManager parameter. final static UnitunregisterApp(Context context, String instance)final static List<String>getDistributors(Context context)Get a list of available distributors installed on the system final static UnittryUseDefaultDistributor(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 applicationsExternal distributors will be favored over embedded distributors. final static UnittryUseCurrentOrDefaultDistributor(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. final static UnitsaveDistributor(Context context, String distributor)Save distributor as the new distributor to use final static StringgetSavedDistributor(Context context)Get the distributor registered by the user, but the distributor may not have respond yet to our requests. final static StringgetAckDistributor(Context context)Get the distributor registered by the user, and the distributor has already respond to our requestsWill call MessagingReceiver.onUnregistered for all instances if the distributor is not installed anymore. final static UnitremoveDistributor(Context context)Unregister all instances and remove the distributor final static UnitremoveDistributor(Context context, KeyManager keyManager)removeDistributor with additional KeyManager parameter. final static UnitforceRemoveDistributor(Context context)-
-
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).
-
register
final static Unit register(Context context, String instance, String messageForDistributor, String vapid, KeyManager keyManager)
register with additional KeyManager parameter.
- Parameters:
keyManager- To manager web push keys.
-
registerApp
@Deprecated(message = Renamed, replaceWith = @ReplaceWith(imports = {}, expression = UnifiedPush.register(context, instance, messageForDistributor, vapid))) final static Unit registerApp(Context context, String instance, String messageForDistributor, String vapid)
-
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.
-
unregister
final static Unit unregister(Context context, String instance, KeyManager keyManager)
unregister with additional KeyManager parameter.
- Parameters:
keyManager- To manager web push keys.
-
unregisterApp
@Deprecated(message = Renamed, replaceWith = @ReplaceWith(imports = {}, expression = UnifiedPush.unregister(context, instance))) final static Unit unregisterApp(Context context, String instance)
-
getDistributors
final static List<String> getDistributors(Context context)
Get a list of available distributors installed on the system
-
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 withfalsecallback- 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 withfalsecallback- 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
-
getSavedDistributor
final static String getSavedDistributor(Context context)
Get the distributor registered by the user, but the distributor may not have respond yet to our requests. Most of the time getAckDistributor is preferred.
Will call MessagingReceiver.onUnregistered for all instances if the distributor is not installed anymore.
-
getAckDistributor
final static String getAckDistributor(Context context)
Get the distributor registered by the user, and the distributor has already respond to our requests
Will call MessagingReceiver.onUnregistered for all instances if the distributor is not installed anymore.
-
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.
-
removeDistributor
final static Unit removeDistributor(Context context, KeyManager keyManager)
removeDistributor with additional KeyManager parameter.
- Parameters:
keyManager- To manager web push keys.
-
forceRemoveDistributor
@Deprecated(message = Renamed, replaceWith = @ReplaceWith(imports = {}, expression = UnifiedPush.removeDistributor(context))) final static Unit forceRemoveDistributor(Context context)
-
-
-
-