Flutter
An example application is available to show basic usage of the library.
Add the following code to your pubspec.yaml.
unifiedpush: ^5.0.0
You can ignore instances if you don’t need to handle multiple connections.
UnifiedPush.initialize(
onNewEndpoint: onNewEndpoint,
onRegistrationFailed: onRegistrationFailed,
onUnregistered: onUnregistered,
onMessage: onMessage,
);
void onNewEndpoint(String endpoint, String instance) {
// You should send the endpoint to your application server
// and sync for missing notifications.
}
void onRegistrationFailed(String instance) {}
void onUnregistered(String instance) {}
void onMessage(Uint8List message, String instance) {}
To register for receiving push services you have two options, after initializing:
- Have the library handle distributor selection
// Call the library function
UnifiedPush.registerAppWithDialog(
context,
instance, // Optionnal String, to get multiple endpoints (one per instance)
[featureAndroidBytesMessage] // Optionnal String Array with required features
);
- Handle selection yourself
// Check if a distributor is already registered
if (await UnifiedPush.getDistributor() != "") {
// Re-register in case something broke
UnifiedPush.registerApp(
instance, // Optionnal String, to get multiple endpoints (one per instance)
[featureAndroidBytesMessage] // Optionnal String Array with required features
);
} else {
// Get a list of distributors that are available
final distributors = await UnifiedPush.getDistributors(
[featureAndroidBytesMessage] // Optionnal String Array with required features
);
// select one or show a dialog or whatever
final distributor = myPickerFunc(distributors);
// save the distributor
UnifiedPush.saveDistributor(distributor);
// register your app to the distributor
UnifiedPush.registerApp(
instance, // Optionnal String, to get multiple endpoints (one per instance)
[featureAndroidBytesMessage] // Optionnal String Array with required features
);
}
// inform the library that you would like to unregister from receiving push messages
UnifiedPush.unregister(
instance // Optionnal String, to get multiple endpoints (one per instance)
);
// You won't receive onUnregistered for this instance
(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."
On the Android side, you will need to import and declare the embedded distributor. Please refer to Embedded FCM Distributor for more information.