Load Activity Feed content¶
Activity Feed consists of posts and announcements. Each post and announcement have a list of comments and likes.
In the following guide, we’ll show how to query all sorts of Activity Feed content.
Save time with prebuilt Activity Feed UI
Let your users post, comment and like on your Activity Feed with one line of code with prebuilt UI. Check Activity Feed UI guide for more details.
Prerequisite¶
Load posts¶
To get a list of posts on the Activity Feed, you have to create a query and call API to get the results:
val query: ActivitiesQuery = ... // Create query
val pagingQuery = PagingQuery(query)
Communities.getActivities(pagingQuery, { result: PagingResult<GetSocialActivity> ->
val activities = result.entries
Log.d("Communities", "Activities: $activities")
}, { error: GetSocialError ->
Log.d("Communities", "Failed to get activities: $error")
})
let query: ActivitiesQuery = ... // Create query
let pagingQuery = ActivitiesPagingQuery(query)
Communities.activities(pagingQuery, success: { result in
print("Activities: \(result.activities)")
}, failure: { error in
print("Failed to get activities: \(error)")
})
ActivitiesQuery query = ... // Create query
var pagingQuery = new PagingQuery<ActivitiesQuery>(query);
Communities.GetActivities(pagingQuery,
(result) => {
Debug.Log("Activities: " + result.Entries);
},
(error) => {
Debug.Log("Failed to get activities: " + error);
});
var query = ... ; // Create query
var pagingQuery = PagingQuery(query);
Communities.getActivities(pagingQuery)
.then((result) {
var activities = result.entries;
print('Activities: $activities');
}).catchError((error) => { print('Failed to get activities, error: $error') });
var query = ... ; // Create query
var pagingQuery = new PagingQuery(query);
Communities.getActivities(pagingQuery)
.then((result) => {
var activities = result.entries;
console.log('Activities: ' + activities);
}, (error) => {
console.log('Failed to get activities, error: ' + error.message);
});
const query = ... ; // Create query
GetSocialSDK.Communities.getActivities(new GetSocialSDK.PagingQuery(query))
.then((result) => {
console.log('Activities: ', result.entries);
}, (error) => {
console.log('Failed to get activities, error: ', error);
});
This method uses the PagingQuery
concept that is used across our SDK. Read more about this.
Create a query¶
To create query you have to specify for which feed you want to load posts.
Activities in Topic Feed¶
To get all activities in topic "cats"
:
val query = ActivitiesQuery.activitiesInTopic("cats")
let query = ActivitiesQuery.inTopic("cats")
var query = ActivitiesQuery.ActivitiesInTopic("cats");
var query = ActivitiesQuery.inTopic('cats');
const query = ActivitiesQuery.inTopic('cats');
const query = GetSocialSDK.ActivitiesQuery.inTopic('cats');
Activities from All Topics¶
To get activities from all topics:
val query = ActivitiesQuery.inAllTopics()
let query = ActivitiesQuery.inAllTopics()
var query = ActivitiesQuery.InAllTopics();
var query = ActivitiesQuery.inAllTopics();
const query = ActivitiesQuery.inAllTopics();
const query = GetSocialSDK.ActivitiesQuery.inAllTopics();
Activities in Group Feed¶
To get all activities in group "clan_x"
:
val query = ActivitiesQuery.activitiesInGroup("clan_x")
let query = ActivitiesQuery.inGroup("clan_x")
var query = ActivitiesQuery.ActivitiesInGroup("clan_x");
var query = ActivitiesQuery.inGroup('clan_x');
const query = ActivitiesQuery.inGroup('clan_x');
const query = GetSocialSDK.ActivitiesQuery.inGroup('clan_x');
Recent Comments¶
Sometimes you want to also show some of the recent comments embedded in a list of posts that you retrieve.
This is possible by setting a property in the activity query that you are building to define how many of those recent comments (max 3) you want to get for each of the activities in the response. By default, no recent comments will be included in the response.
For example, if you are retrieving activities from a group as before and want to also get the 2 most recents comments for each activity, you can set it up like this:
val query = ActivitiesQuery.activitiesInGroup("clan_x").includeComments(3)
let query = ActivitiesQuery.inGroup("clan_x").includeComments(3)
Not yet available
var query = ActivitiesQuery.inGroup('clan_x').includeComments(3);
const query = ActivitiesQuery.inGroup('clan_x').includeComments(3);
const query = GetSocialSDK.ActivitiesQuery.inGroup('clan_x').includeComments(3);
Comments¶
You can also get all the comments to a certain activity by providing the parent activity ID.
This can be done for posts, announcements and even for for comments, useful if you want to support nested comments inside your community.
val query = ActivitiesQuery.commentsToActivity(activityId)
let query = ActivitiesQuery.commentsToActivity(activityId)
var query = ActivitiesQuery.CommentsToActivity(activityId);
var query = ActivitiesQuery.commentsToActivity(activityId);
const query = ActivitiesQuery.commentsToActivity(activityId);
const query = GetSocialSDK.ActivitiesQuery.commentsToActivity(activityId);
User Feed¶
To get the feed of the user with id 42
:
val query = ActivitiesQuery.feedOf(UserId.create("42"));
let query = ActivitiesQuery.feedOf(UserId.create("42"))
var query = ActivitiesQuery.FeedOf(UserId.Create("42"));
var query = ActivitiesQuery.feedOf(UserId.create('42'));
const query = ActivitiesQuery.feedOf(UserId.create('42'));
const query = GetSocialSDK.ActivitiesQuery.feedOf(GetSocialSDK.UserId.create('42'));
Activities from All Feeds¶
To get activities from all existing feeds, including topics and user feeds:
val query = ActivitiesQuery.everywhere()
let query = ActivitiesQuery.everywhere()
var query = ActivitiesQuery.Everywhere();
var query = ActivitiesQuery.everywhere();
const query = ActivitiesQuery.everywhere();
const query = GetSocialSDK.ActivitiesQuery.everywhere();
Filter Activities by Tag¶
To get activities from all feeds with a tag "cat"
:
val query = ActivitiesQuery.everywhere().withTag("cat")
let query = ActivitiesQuery.everywhere().withTag("cat")
var query = ActivitiesQuery.Everywhere().WithTag("cat");
var query = ActivitiesQuery.everywhere().withTag('cat');
const query = ActivitiesQuery.everywhere().withTag('cat');
const query = GetSocialSDK.ActivitiesQuery.everywhere().withTag('cat');
Filter Activities by Labels¶
To get activities from all feeds with a label "cat"
:
var labels: Array<String> = ...
val query = ActivitiesQuery.everywhere().withLabels(labels)
let query = ActivitiesQuery.everywhere().withLabels(["cat"])
ActivitiesQuery query = ActivitiesQuery.everywhere().withLabels(['cat']);
const query = ActivitiesQuery.everywhere().withLabels(['cat'])
const query = GetSocialSDK.ActivitiesQuery.everywhere().withLabels(['cat'])
Filter Activities by Author¶
To get activities from all feeds created by a user with id 42
:
val query = ActivitiesQuery.everywhere().byUser(UserId.create("42"))
let query = ActivitiesQuery.everywhere().byUser(UserId.create("42"))
var query = ActivitiesQuery.Everywhere().ByUser(UserId.Create("42"));
ActivitiesQuery query = ActivitiesQuery.everywhere().byUser(UserId.create('42'));
const query = ActivitiesQuery.everywhere().byUser(UserId.create('42'));
const query = GetSocialSDK.ActivitiesQuery.everywhere().byUser(GetSocialSDK.UserId.create('42'));
Trending activities¶
It is possible to get trending activities:
val query = ActivitiesQuery.everywhere().onlyTrending(true)
let query = ActivitiesQuery.everywhere().onlyTrending(true)
var query = ActivitiesQuery.everywhere().OnlyTrending(true);
var query = ActivitiesQuery.everywhere().onlyTrending(true);
const query = ActivitiesQuery.everywhere().onlyTrending(true);
const query = GetSocialSDK.ActivitiesQuery.everywhere().onlyTrending(true);
Load announcements¶
To get a list of announcements that are active now:
// To get announcements for topic
val query = AnnouncementsQuery.topic("cats")
// Or to get announcements only posted to all feeds
val query = AnnouncementsQuery.timeline()
Communities.getAnnouncements(query, { announcements: List<GetSocialActivity> ->
Log.d("Communities", "Announcements: $announcements")
}, { error: GetSocialError ->
Log.d("Communities", "Failed to get announcements: $error")
})
// To get announcements for topic
let query = AnnouncementsQuery.inTopic("cats")
// Or to get announcements only posted to all feeds
let query = AnnouncementsQuery.timeline()
Communities.announcements(query, success: { announcements in
print("Announcements: \(announcements)")
}, failure: { error in
print("Failed to get announcements: \(error)")
})
// To get announcements for topic
var query = AnnouncementsQuery.InTopic("cats");
// Or to get announcements only posted to all feeds
var query = AnnouncementsQuery.Timeline();
Communities.GetAnnouncements(query,
(result) => {
Debug.Log("Announcements: " + result);
},
(error) => {
Debug.Log("Failed to get announcements: " + error);
});
// To get announcements for topic
AnnouncementsQuery query = AnnouncementsQuery.inTopic('cats');
// Or to get announcements only posted to all feeds
AnnouncementsQuery query = AnnouncementsQuery.timeline();
Communities.getAnnouncements(query)
.then((value) => print('Announcements: $value'))
.catchError((error) => print('Failed to get announcements, error: $error'));
// To get announcements for topic
const query = AnnouncementsQuery.inTopic('cats');
// Or to get announcements only posted to all feeds
const query = AnnouncementsQuery.timeline();
Communities.getAnnouncements(query)
.then((value) => {
console.log('Announcements: ' + value);
}, (error) => {
console.log('Failed to get announcements, error: ' + error);
});
// To get announcements for topic
const query = GetSocialSDK.AnnouncementsQuery.inTopic('cats');
// Or to get announcements only posted to all feeds
const query = GetSocialSDK.AnnouncementsQuery.timeline();
GetSocialSDK.Communities.getAnnouncements(query)
.then((announcements) => {
console.log('Announcements: ', announcements);
}, (error) => {
console.log('Failed to get announcements, error: ', error);
});
Load Reactions¶
For each activity post, announcement and comment you can check the reactions count, reactions of the current user and get the list of users who reacted to the activity:
val activityPost: GetSocialActivity = ... // your activity
// to check a particular reaction, e.g. "like"
val isLikedByMe = activityPost.myReactions.contains(Reactions.LIKE)
val likesCount = activityPost.reactionsCount[Reactions.LIKE]
val query = ReactionsQuery.forActivity(activityPost.id)
val pagingQuery = PagingQuery(query)
Communities.getReactions(pagingQuery, { result: PagingResult<UserReaction> ->
val reactions = result.entries
Log.d("Communities", "Reactions: $reactions")
}, { error: GetSocialError ->
Log.d("Communities", "Failed to get reactions: $error")
})
let activity: Activity = ... // your activity
// to check a particular reaction, e.g. "like"
let isLikedByMe = activity.myReactions.contains(Reactions.like)
let likesCount = activity.reactionsCount[Reactions.like]
let query = ReactionsQuery.forActivity(activity.id)
let pagingQuery = ReactionsPagingQuery(query)
Communities.reactions(pagingQuery, success: { result in
print("Reactions: \(result.reactions)")
}, failure: { error in
print("Failed to get reactions: \(error)")
})
Activity activity = ... // your activity
var isLikedByMe = activity.MyReactions.Contains(Reactions.Like);
var likesCount = activity.GetReactionsCount(Reactions.Like);
var query = ReactionsQuery.ForActivity(activity.Id);
var pagingQuery = new PagingQuery<ReactionsQuery>(query);
Communities.GetReactions(pagingQuery,
(result) => {
Debug.Log("Reactions: " + result.Entries);
},
(error) => {
Debug.Log("Failed to get reactions: " + error);
});
Activity activity = ...; // your activity
// check a particular reaction, e.g. 'like'
var isLikedByMe = activity.myReactions.contains('like');
var likesCount = activity.reactionsCount['like'];
var query = ReactionsQuery.forActivity(activity.id);
var pagingQuery = PagingQuery(query);
Communities.getReactions(pagingQuery)
.then((result) {
var reactions = result.entries;
print('Reactions: $reactions');
})
.catchError((error) => print('Failed to get reactions, error: $error'));
const activity = ...; // your activity
// check a particular reaction, e.g. 'like'
const isLikedByMe = activity.myReactions.contains('like');
const likesCount = activity.reactionsCount['like'];
const query = ReactionsQuery.forActivity(activity.id);
const pagingQuery = new PagingQuery(query);
Communities.getReactions(pagingQuery)
.then((result) => {
var reactions = result.entries;
console.log('Reactions: ' + reactions);
}, (error) => {
console.log('Failed to get reactions, error: ' + error);
});
const activity = ...; // your activity
// check a particular reaction, e.g. 'like'
const isLikedByMe = activity.myReactions.contains('like');
const likesCount = activity.reactionsCount['like'];
const query = GetSocialSDK.ReactionsQuery.forActivity(activity.id);
GetSocialSDK.Communities.getReactions(new GetSocialSDK.PagingQuery(query))
.then((result) => {
console.log('Reactions: ', result.entries);
}, (error) => {
console.log('Failed to get reactions, error: ', error);
});
This method uses the PagingQuery
concept that is used across our SDK. Read more about this.
Working with activity¶
The section below explains how you can display Activity after loading it from GetSocial API.
Learn what can you do with activity object in Android and iOS.
Post Author and Creation Date¶
Each post, announcement, and comment contains a set of metadata that you can use on your UI:
var activity: GetSocialActivity
val createdAt: Long = activity.createdAt // Creation date, UNIX timestamp in seconds
val author: User = activity.author
let activity: Activity = ... // your activity
let createdAt = activity.createdAt // Creation date, UNIX timestamp in seconds
let author = activity.author
Activity activity = ...; // your activity
var createdAt = activity.CreatedAt; // Creation date, UNIX timestamp in seconds
var author = activity.Author;
Activity activity = ... // your activity
var createdAt = activity.createdAt; // Creation date, UNIX timestamp in seconds
var author = activity.author;
Activity activity = ... // your activity
const createdAt = activity.createdAt; // Creation date, UNIX timestamp in seconds
const author = activity.author;
Activity activity = ... // your activity
const createdAt = activity.createdAt; // Creation date, UNIX timestamp in seconds
const author = activity.author;
Text¶
Text content can contain text itself, mentions, tags, and URLs.
User ids from mentions in the text are replaced by API to user display names. If you posted text like Hello @0128309218!
where @0128309218
is user id, API returns text like Hello Ben!
.
Optionally you can make URLs on your UI clickable.
val contentText: TextView = ... // Get instance of text view
val post : GetSocialActivity = ... // Instance of activity post, announcement or comment
if (post.text != null {
val textContent = post.text
contentText.text = textContent
// Don't forget to make text views accessible
contentText.contentDescription = textContent
// Optionally, make links clickable if post was made from Dashboard
if (post.author.isVerified) {
Linkify.addLinks(contentText, Linkify.WEB_URLS)
}
}
let contentText: UITextView = ... // Get the instance of text view
let activity: Activity = ... // Instance of activity post, announcement or comment
guard let activityText = activity.text else {
return
}
contentText.text = activityText.text
// Don't forget to make text views accessible
contentText.accessibilityValue = activityText
// Optionally, make links clickable if post was made from Dashboard
if activity.author.isVerified {
contentText.dataDetectorTypes = .link
}
Activity activity = ... // your activity
var activityText = activity.Text;
Activity activity = ...; // your activity
var activityText = activity.text;
Activity activity = ...; // your activity
const activityText = activity.text;
Activity activity = ...; // your activity
const activityText = activity.text;
Mentions¶
To highlight mentions in the text and make them clickable:
val spannable = SpannableString(post.text)
post.mentions.forEach { mention ->
val startIndex = mention.startIndex
val endIndex = mention.endIndex
spannable.setSpan(BackgroundColorSpan(Color.YELLOW), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(ForegroundColorSpan(Color.BLUE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan({ widget: View ->
// handle click
}, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
contentText.text = spannable
let mutableAttributedString = NSMutableAttributedString(string: activity.text!)
activity.mentions.forEach {
var effectiveRange = NSRange(location: Int($0.startIndex), length: Int($0.endIndex - $0.startIndex))
mutableAttributedString.addAttributes([
NSAttributedString.Key.backgroundColor: UIColor.yellow,
NSAttributedString.Key.foregroundColor: UIColor.blue
], range: effectiveRange)
}
contentText.attributedText = mutableAttributedString
Activity activity = ... // your activity
var mentions = activity.Mentions;
mentions.ForEach(mention => {
Debug.Log("Mention: " + mention);
});
Activity activity = ...; // your activity
activity.mentions.forEach((mention) => print('Mention: $mention') );
const activity = ...; // your activity
activity.mentions.forEach((mention) => {
console.log('Mention: ' + mention');
});
const activity = ...; // your activity
activity.mentions.forEach((mention) => {
console.log('Mention: ', mention');
});
Tags¶
To highlight tags in the text and make them clickable, you may use code like:
val spannable = SpannableString(post.text)
val matcher = Pattern.compile("(#[\\p{L}\\d_]*\\p{L}[\\p{L}\\d_]*)").matcher(spannable)
while (matcher.find()) {
val startIndex = mention.startIndex
val endIndex = mention.endIndex
spannable.setSpan(BackgroundColorSpan(Color.YELLOW), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(ForegroundColorSpan(Color.BLUE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan({ widget: View ->
// handle click
}, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
let mutableAttributedString = NSMutableAttributedString(string: activity.text!)
let pattern = "(#[\\p{L}\\d_]*\\p{L}[\\p{L}\\d_]*)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
regex.enumerateMatches(in: activity.text!, options: [], range: NSRange(location: 0, length: activity.text!.count), using: { result, flags, stop in
if let effectiveRange = result?.range {
mutableAttributedString.addAttributes([
NSAttributedString.Key.backgroundColor: UIColor.yellow,
NSAttributedString.Key.foregroundColor: UIColor.blue
], range: effectiveRange)
}
})
contentText.attributedText = mutableAttributedString
Media attachments¶
Activity Post can contain images and videos. GIFs are converted to videos for optimization. All videos are encoded in MP4.
Check Media Attachments guide for more details.
You can get a list of media attachments, which is empty if you haven’t attached any media to the post. Attachments have the same order as they were posted with.
Action buttons¶
The code below shows how to display the button and implement a combination of custom action handling with the one provided by GetSocial.
Check GetSocial Actions guide for Android, iOS or Unity for more information.
val button : Button = ... // An instance of button for the activity post
val actionButton = post.button
if (actionButton != null) {
button.text = actionButton.title
// Don't forget to make view accessible
button.setContentDescription(actionButton.title)
button.setOnClickListener { view ->
val action = actionButton.action
if ("my-custom-type".equals(action.getType())) {
// Do the custom action handling
} else {
GetSocial.handle(action) // Let GetSocial handle the action
}
}
}
let actionButton: UIButton = ... // An instance of button for the activity post
if let buttonTitle = activity.button?.title {
actionButton.setTitle(buttonTitle, for: .normal)
// Don't forget to make view accessible
actionButton.accessibilityValue = buttonTitle
actionButton.tag = activities.index(of: activity)
let actionButtonTap = UITapGestureRecognizer(target: self, action: #selector(self.didClickActionButton(_:)))
actionButtonTap.numberOfTapsRequired = 1
actionButton.addGestureRecognizer(actionButtonTap)
}
@objc
func didClickActionButton(_ sender: UITapGestureRecognizer?) {
guard let index = sender?.view?.tag else {
return
}
let activity: Activity = activities[index]
if activity.button?.action.type == "my-custom-type" {
// Do the custom action handling
} else {
GetSocial.handle(activity.button?.action)
}
}
Activity activity = ...;
if(activity.Button.Action.Type.Equals("my-custom-type"))
{
// do the custom action handling
} else
{
GetSocial.Handle(activity.Button.Action);
}
Activity activity = ...; // your activity
if (activity.button.action.type == 'my-custom-type') {
// handle custom action
}
const activity = ...; // your activity
if (activity.button.action.type == 'my-custom-type') {
// handle custom action
}
const activity = ...; // your activity
if (activity.button.action.type == 'my-custom-type') {
// handle custom action
}
Source¶
You can check where the activity was posted by checking their source
property:
val activity: GetSocialActivity
val source = activity.source
when(source.type) {
CommunitiesEntityType.TOPIC -> {
// posted to topic with id `source.id`
}
CommunitiesEntityType.ACTIVITY -> {
// activity is a comment to another activity with id `source.id`
}
CommunitiesEntityType.USER -> {
// activity is posted into user's feed og the user with id `source.id.
}
}
let activity: GetSocialActivity
let source = activity.source
switch source.type {
case .topic:
// posted to topic with id `source.id`
case .activity:
// activity is a comment to another activity with id `source.id`
case .user:
// activity is posted into feed of the user with id `source.id`
}
var source = activity.Source;
switch(source.Type)
{
case CommunitiesEntityType.Topic:
// posted to topic with id `source.id`
break;
case CommunitiesEntityType.Activity:
// activity is a comment posted to activity with id `source.id`
break;
case CommunitiesEntityType.User:
// activity is posted to feed of user with id `source.id`
break;
}
Activity activity = ...; // your activity
var source = activity.source;
switch (source.type) {
case CommunitiesEntityType.Topic:
// posted to topic with id `source.id`
break;
case CommunitiesEntityType.Activity:
// activity is a comment to another activity with id `source.id`
break;
case CommunitiesEntityType.User:
// activity is posted into feed of the user with id `source.id`
break;
}
const activity = ...; // your activity
const source = activity.source;
switch (source.type) {
case CommunitiesEntityType.Topic:
// posted to topic with id `source.id`
break;
case CommunitiesEntityType.Activity:
// activity is a comment to another activity with id `source.id`
break;
case CommunitiesEntityType.User:
// activity is posted into feed of the user with id `source.id`
break;
}
const activity = ...; // your activity
const source = activity.source;
switch (source.type) {
case GetSocialSDK.CommunitiesEntityType.Topic:
// posted to topic with id `source.id`
break;
case GetSocialSDK.CommunitiesEntityType.Activity:
// activity is a comment to another activity with id `source.id`
break;
case GetSocialSDK.CommunitiesEntityType.User:
// activity is posted into feed of the user with id `source.id`
break;
}
Next steps¶
- Learn how to add Activity Feed to your app in one line of code with prebuild UI.
- Setup Push Notifications so users get notified on social interactions on the Activity Feed on Android, iOS or Unity.
- Understand how your users use Activity Feed with Analytics.