Android - Implementing Device Registration
In this guide we will dive deeper into how you should handle the device registration. When you launch()
Notificare for the first time, the device will be registered as a non-push device.
When a successful device registration takes place, we will emit an intent. If you have configured a custom intent receiver as mentioned in the Implementation page, you can receive those intents by overriding the onDeviceRegistered()
method from NotificareIntentReceiver
.
- Kotlin
- Java
class CustomIntentReceiver : NotificareIntentReceiver() {
override fun onDeviceRegistered(context: Context, device: NotificareDevice) {
}
}
class CustomIntentReceiver extends NotificareIntentReceiver {
@Override
protected void onDeviceRegistered(@NonNull Context context, @NonNull NotificareDevice device) {
}
};
You can also check the details of the currently registered device.
- Kotlin
- Java
Notificare.device().currentDevice
NotificareDeviceCompat.getCurrentDevice();
The onReady()
method in NotificareIntentReceiver
will also be called when all Notificare modules have been launched, including a successful device registration.
Additionally, you can verify whether Notificare is ready at any point by calling Notificare.isReady
or listen to the onReady
event anywhere in your code by adding a Notificare.OnReadyListener
as described below.
- Kotlin
- Java
class MainActivity : AppCompatActivity(), Notificare.Listener {
override fun onCreate(savedInstanceState: Bundle?) {
// more code ...
Notificare.addListener(this)
}
override fun onDestroy() {
// more code ...
Notificare.removeListener(this)
}
// region Notificare.OnReadyListener
override fun onReady(application: NotificareApplication) {
}
override fun onUnlaunched() {
}
// endregion
}
public class MainActivity extends AppCompatActivity implements Notificare.Listener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// more code ...
Notificare.addListener(this);
}
@Override
protected void onDestroy() {
// more code ...
Notificare.removeListener(this);
}
// region Notificare.OnReadyListener
@Override
public void onReady(@NonNull NotificareApplication application) {
}
@Override
public void onUnlaunched() {
}
// endregion
}
Assign a user to the device
By default, a device is registered as an anonymous user. However, to fully use push notifications in your Actito omni-channel strategy, simply registering an anonymous device is not what you want. For example, if you authenticate your users, you will want to assign user information to the registered device as shown below.
- Kotlin
- Java
Notificare.device().updateUser(
userId = "7f42bedc-d74b-4c64-a5cf-76bcc5130b05",
userName = "John Doe",
callback = object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
}
)
String userId = "7f42bedc-d74b-4c64-a5cf-76bcc5130b05";
String userName = "John Doe";
NotificareDeviceCompat.updateUser(userId, userName, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
After that, the device will remain registered with that userId
/ userName
until you explicitly set the user as anonymous. Depending on the way you authenticate users, you might want to check the logged-in state on launch (in the onReady) and change it if necessary. Forcing registration as anonymous can be best achieved by setting userId
and userName
to null
.
Only the userId will be used to match devices to profiles in your Actito DB.
The userName is only used to provided additional information.
- Kotlin
- Java
Notificare.device().updateUser(
userId = null,
userName = null,
callback = object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
}
)
String userId = null;
String userName = null;
NotificareDeviceCompat.updateUser(userId, userName, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
To choose the proper user ID:
- The data must be available as a key in Actito thanks to the data synchronization between your system and Actito.
- The data must be available on your website (through logging into a client space).
Please use an attribute which is already defined as unique on your Actito profile table.
If you need to add a new unique attribute, this is possible, but be careful because it could have impacts on your data synchronization process between your system and Actito. Ask your Integration Data Expert if necessary.
Override Device Language
By default, we will automatically collect the language and region of a device based on the Locale of the device. For most cases this will be enough but for those cases where you would like to override the device language and region combination to a strict selection of languages, you can do so by invoking the following method:
- Kotlin
- Java
Notificare.device().updatePreferredLanguage("en-US", object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareDeviceCompat.updatePreferredLanguage("en_US", new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
Eventually, you can always retrieve the preferred language by calling the following method:
- Kotlin
- Java
Notificare.device().preferredLanguage
NotificareDeviceCompat.getPreferredLanguage();