Skip to content

Receive GetSocial Notifications on iOS

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 it to your user inside the app. Or send it once again as local notification. Or handle it in any other convenience method.

Query

You have two types of queries: GetSocialNotificationQuery and GetSocialNotificationsCountQuery to get notifications list and notifications count respectively.

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

To query notifications by status, use withStatuses:(NSArray<GetSocialNotificationStatus> *)statuses factory method. You can pass one or few statuses from GetSocialNotificationStatus 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:(NSArray<GetSocialNotificationType *> *)types and pass one or few types you want to query.

Notifications Count

To get the count of notifications use [GetSocialUser notificationsCountWithQuery:query success:success failure:failure] method. For example, to get a number of all unread notifications for current user:

```objc tab=”Objective-C”
GetSocialNotificationsCountQuery *query = [GetSocialNotificationsCountQuery withStatuses:@[ GetSocialNotificationStatusRead ]];
[GetSocialUser notificationsCountWithQuery:query success:^(int count) {
NSLog(@”Notifications count is %d”, count);
} failure:^(NSError *error) {
NSLog(@”Failed to get notifications count, %@”, error);
}];

```swift tab="Swift"
let query = GetSocialNotificationsCountQuery.withStatuses( [GetSocialNotificationStatus.unread] )!
GetSocialUser.notificationsCount(with: query, success: { (count:Int32) in
    print("Notifications count is \(count)")
}) { (error : Error) in
    print("Failed to get notifications count, error: \(error.localizedDescription)")
}

Notifications List

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

```objc tab=”Objective-C”
GetSocialNotificationsQuery *query = [GetSocialNotificationsQuery withStatuses:@[ GetSocialNotificationStatusRead ]];
[GetSocialUser notificationsWithQuery:query success:^(NSArray *notifications) {
[self showNotifications:notifications];
} failure:^(NSError *error) {
NSLog(@”Failed to get notifications, %@”, error);
}];

```swift tab="Swift"
let query = GetSocialNotificationsQuery.withStatuses( [GetSocialNotificationStatus.unread] )!
GetSocialUser.notifications(with: query, success: { (notifications:[GetSocialNotification]) in
    self.showNotifications(notifications)
}) { (error : Error) in
    print("Failed to get notifications, error: \(error.localizedDescription)")
}

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 [GetSocialUser setNotificationsStatus:notificationIds status:newStatus success:success failure:failure] method:

```objc tab=”Objective-C”
NSArray *notificationsToBeRead = …;
NSMutableArray *notificationIds = [NSMutableArray new];
for (GetSocialNotification *notification in notificationsToBeRead) {
[notificationIds addObject:notification.notificationId];
}
GetSocialNotificationStatus newStatus = GetSocialNotificationStatusRead;
[GetSocialUser setNotificationsStatus:notificationIds status:newStatus success:^() {
NSLog(@”Successfully changed notifications status”);
} failure:^(NSError *error) {
NSLog(@”Failed to change notifications status, %@”, error);
}];

```swift tab="Swift"
let notifications : [GetSocialNotification] = ...
var notificationIds : [String] = []
notifications.forEach { notification in
    notificationIds.append(notification.notificationId)
}
let newStatus = GetSocialNotificationStatus.read
GetSocialUser.setNotificationsStatus(notificationIds, status: newStatus, success: {
    print("Successfully changed notifications status")
}) { (error : Error) in
    print("Failed to change notifications status, error: \(error.localizedDescription)")
}

You can mark just one notification using @[notification.notificationId].

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 GetSocialNotificationStatusConsumed status. If user wants to ignore it, e.g. decline friends request, set it to GetSocialNotificationStatusIgnored.

Next Steps

Give us your feedback! Was this article helpful?

😀 🙁