Creating a Public Forum

A public forum is a common way to share information with other users. Forums are often organized around a particular subject. The following use case describes how to create public forum functionality in your app and illustrates use of the APIs by creating a channel for the SF Giants.

Create Channel

In Magnet Message, our channel object is how messages are sent to and received by a group. To create a channel (sf_giants):

1
2
3
4
5
6
7
8
9
10
11
12
NSString *channelName = @"sf_giants";
NSString *channelSummary = @"Get the inside scoop on the San Francisco Giants";
[MMXChannel createWithName:channelName
summary:channelSummary
isPublic:YES
publishPermissions:MMXPublishPermissionsSubscribers
success:^(MMXChannel *channel) {
//Different permissions level are MMXPublishPermissionsAnyone,MMXPublishPermissionsSubscribers,MMXPublishPermissionsOwnerOnly
}
failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

NOTE: The creator of the channel is automatically subscribed to the channel.

Discover Channels

Next, discover channels that you can subscribe to

  • To discover channels whose name starts with 'sf':

1
2
3
4
5
6
7
8
9
[MMXChannel channelsStartingWith:@"sf"
limit:10
offset:0
success:^(int totalCount, NSArray *channels) {
// do something
}
failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • To discover all public channels that have been created in the application, use the code below.

    NOTE: The limit and offset allow you to implement pagination with the results.

1
2
3
4
5
6
7
[MMXChannel allPublicChannelsWithLimit:10
offset:0
success:^(int totalCount, NSArray *channels) {
//Success
} failure:^(NSError *) {
//Failure
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • To find a public channel directly with a known name (sf_giants):

1
2
3
4
5
6
7
[MMXChannel channelForName:@"sf_giants"
isPublic:YES
success:^(MMXChannel *channel) {
//Success
} failure:^(NSError *error) {
//Failure
}]
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Check Channel Subscription

After getting a channel, check if the current user is subscribed to the channel:

1
2
3
4
MMXChannel *sfGiantsChannel = ...;
if (sfGiantsChannel.isSubscribed) {
NSLog(@"I'm following the Giants!");
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Subscribe to Channel

To subscribe to a channel and send and receive messages (sfGiants):

1
2
3
4
5
6
MMXChannel *sfGiantsChannel = ...;
[sfGiantsChannel subscribeWithSuccess:^{
// do something
} failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Unsubscribe from Channel

To unsubscribe from a channel (in this case, the sfSeals):

1
2
3
4
5
6
MMXChannel *sfSealsChannel = ...;
[sfSealsChannel unSubscribeWithSuccess:^{
// do something
} failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

View Channel Subscribers

After subscribing, you can see other subscribers to the channel (sfGiants):

1
2
3
4
5
6
MMXChannel *sfGiantsChannel = ...;
[sfGiantsChannel subscribersWithLimit:100 offset:0 success:^(int totalCount, NSArray *subscribers) {
// do something
} failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Get Published Messages on Channel

You can also get all messages published on the channel. Let's retrieve the messages sent from an hour ago until now (limited to up to 10 at a time).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MMXChannel *sfGiantsChannel = ...;
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.hour = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *anHourAgo = [theCalendar dateByAddingComponents:dateComponents
toDate:now
options:0];
[sfGiantsChannel messagesBetweenStartDate:
anHourAgo
endDate:now
limit:10
offset:0
ascending:NO
success:^(int totalCount, NSArray *messages) {
// do something
} failure:^(NSError *error) {
// do something
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Publish Message to Channel

To publish a message to the channel:

1
2
3
4
5
6
7
8
9
MMXChannel *sfGiantsChannel = ...;
NSDictionary *messageContent = @{@"message":@"Go Giants!"};
[sfGiantsChannel publish:
messageContent
success:^(MMXMessage *message) {
NSLog(@"Successfully published to channel %@",sfGiantsChannel.name);
} failure:^(NSError *error) {
NSLog(@"Error publishing to channel %@nError = %@",sfGiantsChannel.name,error);
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Register for Message Notifications

Next, register for incoming message notifications to receive messages from the channels that you are subscribed to:

NOTE: We include the start method to do the necessary initialization before your app starts so you can receive incoming messages.

1
2
3
4
5
6
7
8
9
10
11
12
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Indicate that you are ready to receive messages now!
[MMX start];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didReceiveMessage:)
name:MMXDidReceiveMessageNotification
object:nil];
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Once incoming message notification has been enabled, you can continue to implement your notification code as shown below:

1
2
3
4
5
6
7
8
- (void)didReceiveMessage:(NSNotification *)notification {
MMXMessage *message = notification.userInfo[MMXMessageKey];
// Do something with the message
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:(BOOL)animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Rename Channel

Additionally, a channel can be renamed. To rename a channel, call the rename method on the channel object.

1
2
3
4
5
6
7
MMXChannel *channel;
[channel rename:@"new_name"
success: ^(MMXChannel *channel){
//Channel renaming succeeded!
} failure: ^(NSError *error) {
//Channel renaming failed
}];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

NOTE: Passing in a null name to the channel causes it to be automatically named with this format: “.NO_NAME.SDK_GENERATED_UUID”


Next Steps

See how to create a Private Discussion Group in your app.