Skip to content

Receive GetSocial Notifications on Android

Notification Center with GetSocial

GetSocial provides a data API to implement notification center, similar to what Facebook has, inside your application. All notifications received via API will also generate an associated push notification.

GetSocial Notification Center UI

Let your users receive and interact with GetSocial Notifications in minutes, not days with prebuilt Notification Center UI.

Prerequisite

Receive Notifications

To be sure your users didn’t miss any important (or not) notifications, you can query GetSocial notifications and show them to your user inside the app.

Query

You have two types of queries: NotificationQuery and NotificationsCountQuery to get notifications list and notifications count respectively.

Notification can differ by a read status - be read or unread. Also each notification has type, it is one of Notification.NotificationType constants.

To query notifications by status, use withStatuses(String... statuses) factory method. You can pass one or few statuses from NotificationStatus constants. Use withAllStatuses() to query notifications with any status.

By default notifications of all types are queries. Also you can explicitly call ofAllTypes() to be sure that all types are queries. To specify a list of types you’re interested in call method ofTypes(String... types) and pass one or few types you want to query.

Notifications Count

To get the count of notifications use GetSocial.User.getNotificationsCount(query, callback) method. For example, to get a number of all unread notifications for current user:

```java tab=”Java”
NotificationsCountQuery query = NotificationsCountQuery.withStatuses(NotificationStatus.UNREAD).ofAllTypes();
GetSocial.User.getNotificationsCount(query, new Callback() {
@Override
public void onSuccess(Integer count) {
Log.d(“Notifications”, “Unread notifications count: ” + count);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("Notifications", "Failed to get notifications count: " + exception);
}

});
kotlin tab=”Kotlin”
val query = NotificationsCountQuery.withStatuses(NotificationStatus.UNREAD).ofAllTypes()
GetSocial.User.getNotificationsCount(query, object : Callback {
override fun onSuccess(count: Int) {
println(“Unread notifications count: ${count}”)
}

override fun onFailure(exception: GetSocialException) {
println(“Failed to get notifications count, error: ${exception.message}”)
}
})
```

Notifications List

Similar to notifications count you can fetch a list of notifications using GetSocial.User.getNotifications(query, callback) mehtod:

```java tab=”Java”
NotificationsQuery query = NotificationsQuery.withStatuses(NotificationStatus.UNREAD).ofAllTypes();
GetSocial.User.getNotifications(query, new Callback>() {
@Override
public void onSuccess(List notifications) {
showNotifications(notifications);
}

1
2
3
4
@Override
public void onFailure(GetSocialException exception) {
    Log.e("Notifications", "Failed to get notifications: " + exception);
}

});
kotlin tab=”Kotlin”
val query = NotificationsQuery.withStatuses(NotificationStatus.UNREAD).ofAllTypes()
GetSocial.User.getNotifications(query, object: Callback>{
override fun onSuccess(notifications: List) {
showNotifications(notifications)
}

override fun onFailure(exception: GetSocialException) {
println(“Failed to get notifications: ${exception.message}”)
}
})
```

Notifications Status

Read and Unread

All the notifications that are sent to a user are unread. The only exception is notifications that was clicked by a user - such becomes read automatically.

If you want to set notification read or unread use GetSocial.User.setNotificationsStatus(notificationIds, status, callback) method:

```java tab=”Java”
List notificationsToBeRead = …;
List notificationIds = new ArrayList();
for (Notification notification : notificationsToBeRead) {
notificationIds.add(notification.getId());
}
String newStatus = NotificationStatus.READ;
GetSocial.User.setNotificationsStatus(notificationIds, newStatus, new CompletionCallback() {
@Override
public void onSuccess() {
Log.d(“Notifications”, “Successfully changed notifications status”);
}
@Override
public void onFailure(GetSocialException exception) {
Log.e(“Notifications”, “Failed to change notifications status: ” + exception);
}
});

```kotlin tab="Kotlin"
var notificationsToBeRead : List<Notification> = ...
var notificationIds = notificationsToBeRead.map { notification -> notification.id }
val newStatus = NotificationStatus.READ
GetSocial.User.setNotificationsStatus(notificationIds, newStatus, object: CompletionCallback{
    override fun onSuccess() {
        println("Successfully changed notifications status")
    }

    override fun onFailure(exception: GetSocialException) {
        println("Failed to change notifications status, error: ${exception.message}")
    }
})

You can mark just one notification using Collections.singletonList(notification.getId()).

Consumed and Ignored

If your notification is kind of “consumable” - e.g. friend request, pending action or whatever it could be(check some possible usecase) - you may want to mark it as “consumed” by user, so it won’t appear in the list of read/unread notifications. For this there is NotificationStatus.CONSUMED status. If user wants to ignore it, e.g. decline friends request, set it to NotificationStatus.IGNORED.

Next Steps

Give us your feedback! Was this article helpful?

😀 🙁