Android
An example application is available to show basic usage of the library.
Add the dependency to the app build.gradle. Replace {VERSION} with the last versions (connector, connector-ui).
dependencies {
// ...
implementation 'org.unifiedpush.android:connector:{VERSION}'
// If you wish to use connector-ui dialog
implementation 'org.unifiedpush.android:connector-ui:{VERSION}'
}
Select a distributor and register
To register for receiving push services you have two options:
Unregister
// inform the library that you would like to unregister from receiving push messages
// Options:
// "instance" to delete if used during registration
unregisterApp(context)
// You won't receive onUnregistered for this instance
Select a distributor and register
To register for receiving push services you have two options:
Unregister
// inform the library that you would like to unregister from receiving push messages
UnifiedPush.unregisterApp(
context,
INSTANCE_DEFAULT // instance
);
// You won't receive onUnregistered for this instance
To receive Push Messages you should extend the class MessagingReceiver and implement the following four methods:
class CustomReceiver: MessagingReceiver() {
override fun onMessage(context: Context, message: ByteArray, instance: String) {
// Called when a new message is received. The message contains the full POST body of the push message
}
override fun onNewEndpoint(context: Context, endpoint: String, instance: String) {
// Called when a new endpoint is to be used for sending push messages
// You should send the endpoint to your application server
// and sync for missing notifications.
}
override fun onRegistrationFailed(context: Context, instance: String) {
// called when the registration is not possible, eg. no network
}
override fun onUnregistered(context: Context, instance: String){
// called when this application is unregistered from receiving push messages
}
}
class CustomReceiver extends MessagingReceiver {
public CustomReceiver() {
super();
}
@Override
public void onNewEndpoint(@NotNull Context context, @NotNull String endpoint, @NotNull String instance) {
// Called when a new endpoint be used for sending push messages
}
@Override
public void onRegistrationFailed(@NotNull Context context, @NotNull String instance) {
// called when the registration is not possible, eg. no network
}
@Override
public void onUnregistered(@NotNull Context context, @NotNull String instance) {
// called when this application is unregistered from receiving push messages
}
@Override
public void onMessage(@NotNull Context context, @NotNull byte[] message, @NotNull String instance) {
// Called when a new message is received. The message contains the full POST body of the push message
}
}
You will also need to declare the receiver in your manifest:
<receiver android:exported="true" android:enabled="true" android:name=".CustomReceiver">
<intent-filter>
<action android:name="org.unifiedpush.android.connector.MESSAGE"/>
<action android:name="org.unifiedpush.android.connector.UNREGISTERED"/>
<action android:name="org.unifiedpush.android.connector.NEW_ENDPOINT"/>
<action android:name="org.unifiedpush.android.connector.REGISTRATION_FAILED"/>
</intent-filter>
</receiver>
(From the application server)
To send a message to an application you need the “endpoint”. You get it in the onNewEndpoint method once it is available. You can then use it to send a message using for example curl. The POST body is the message received by the function onMessage.
curl -X POST "$endpoint" --data "Any message body that is desired."
Please refer to Embedded FCM Distributor for more information.