Inbox
In this page you'll learn how to build an app inbox with all the options at your disposal to create a great messaging experience for your users.
With our library it's extremely easy to implement an in-app inbox. Implementing an inbox increases considerably the engagement rate of your notifications simply because messages will always be available inside your app. To activate the inbox functionality, please follow the instructions described here.
After activating this functionality you can start implementing your inbox in any activity or fragment of your app.
Device-level inbox
The standard approach is to use the device-level inbox. Each device will have its own independent inbox, regardless of the associated user.
The device-level inbox is available via the re.notifica:notificare-inbox
module.
The inbox itself is exposed as LiveData
, so it is really easy to hook up your activities or fragments to a list of inbox items, for example:
Notificare.inbox().observableItems.observe(this, { items ->
// Do something with the items.
})
NotificareInboxCompat.getObservableItems();
// Do something with the items.
In the same way, you can listen for the number of unread messages.
Notificare.inbox().observableBadge.observe(this, { badge ->
// Do something with the badge.
})
NotificareInboxCompat.getObservableBadge();
// Do something with the badge.
If you don't want to use LiveData
, you can still get the items and the badge in the inbox.
// Items
Notificare.inbox().items
// Badge
Notificare.inbox().badge
// Items
NotificareInboxCompat.getItems();
// Badge
NotificareInboxCompat.getBadge();
Managing inbox items
Assuming that you hold a reference to an item, to open an inbox item you would simply do something like this:
val item: NotificareInboxItem
Notificare.inbox().open(item, object : NotificareCallback<NotificareNotification> {
override fun onSuccess(result: NotificareNotification) {
// Optional: Present the notification with Push UI.
Notificare.pushUI().presentNotification(activity, notification)
}
override fun onFailure(e: Exception) {
}
})
NotificareInboxItem item;
NotificareInboxCompat.open(item, new NotificareCallback<NotificareNotification>() {
@Override
public void onSuccess(NotificareNotification result) {
// Optional: Present the notification with Push UI.
NotificarePushUICompat.presentNotification(activity, notification);
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
To remove items from the inbox you would invoke the following method:
val item: NotificareInboxItem
Notificare.inbox().remove(item, object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareInboxItem item;
NotificareInboxCompat.remove(item, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
Additionally, you can also mark a message as read by invoking the following method:
val item: NotificareInboxItem
Notificare.inbox().markAsRead(item, object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareInboxItem item;
NotificareInboxCompat.markAsRead(item, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
Or mark all messages as read by invoking the following method:
Notificare.inbox().markAllAsRead(object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareInboxCompat.markAllAsRead(new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
To remove all items in the inbox you would do the following:
Notificare.inbox().clear(object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareInboxCompat.clear(new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
User-level inbox
By enabling the user inbox, you can have a unique inbox for your users, regardless of how many devices they may have.
The user-level inbox is available via the re.notifica:notificare-user-inbox
module.
There are some requirements to use this functionality, namely:
- Set the User Registration to API Level via the Dashboard;
- Enable the user-based inbox setting via the Dashboard;
- Register your users via the API instead of the SDK.
Given that user authentication must take place in your API, you'll have to perform some actions through it.
Fetching the user inbox
The inbox must be fetched through your API, but we provide a data model to assist you in decoding the response.
Assuming your API relays the response given by the Notificare REST API, you can use the NotificareUserInboxResponse
data model to take care of the decoding.
Since the data model contains a fromJson(JSONObject)
method and a Moshi @JsonClass
generated adapter, it is reasonably simple to integrate it with your API client (i.e., OkHttp).
To illustrate the request, below is a simple example using OkHttp.
val client = OkHttpClient.Builder()
.build()
val request = Request.Builder()
.url("https://{{YOUR_API}}/user-inbox")
.header("Authorization", "Bearer {{token}}")
.build()
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(NotificareUserInboxResponse::class.java)
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
val userInboxResponse = adapter.fromJson(response.body!!.source())
}
override fun onFailure(call: Call, e: IOException) {
// handle the error
}
})
OkHttpClient client = new OkHttpClient.Builder()
.build();
Request request = new Request.Builder()
.url("https://{{YOUR_API}}/user-inbox")
.header("Authorization", "Bearer {{token}}")
.build();
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<NotificareUserInboxResponse> adapter = moshi.adapter(NotificareUserInboxResponse.class);
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
NotificareUserInboxResponse userInboxResponse = adapter.fromJson(response.body().source());
}
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
// handle the error
}
});
Managing inbox items
Assuming that you hold a reference to an item, to open an inbox item you would simply do something like this:
val item: NotificareUserInboxItem
Notificare.userInbox().open(item, object : NotificareCallback<NotificareNotification> {
override fun onSuccess(result: NotificareNotification) {
// Optional: Present the notification with Push UI.
Notificare.pushUI().presentNotification(activity, notification)
}
override fun onFailure(e: Exception) {
}
})
NotificareUserInboxItem item;
NotificareUserInboxCompat.open(item, new NotificareCallback<NotificareNotification>() {
@Override
public void onSuccess(NotificareNotification result) {
// Optional: Present the notification with Push UI.
NotificarePushUICompat.presentNotification(activity, notification);
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
To remove items from the inbox you would invoke the following method:
val item: NotificareUserInboxItem
Notificare.userInbox().remove(item, object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareUserInboxItem item;
NotificareUserInboxCompat.remove(item, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
Additionally, you can also mark a message as read by invoking the following method:
val item: NotificareUserInboxItem
Notificare.userInbox().markAsRead(item, object : NotificareCallback<Unit> {
override fun onSuccess(result: Unit) {
}
override fun onFailure(e: Exception) {
}
})
NotificareUserInboxItem item;
NotificareUserInboxCompat.markAsRead(item, new NotificareCallback<Unit>() {
@Override
public void onSuccess(Unit result) {
}
@Override
public void onFailure(@NonNull Exception e) {
}
});
To mark all messages as read or to remove all items, you must do so through your API. Check with your API team for the integration details. For more information, you can check our REST API documentation.