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
):
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
}];
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':
[MMXChannel channelsStartingWith:@"sf"
limit:10
offset:0
success:^(int totalCount, NSArray *channels) {
// do something
}
failure:^(NSError *error) {
// do something
}];
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.
[MMXChannel allPublicChannelsWithLimit:10
offset:0
success:^(int totalCount, NSArray *channels) {
//Success
} failure:^(NSError *) {
//Failure
}
- To find a public channel directly with a known name (
sf_giants
):
[MMXChannel channelForName:@"sf_giants"
isPublic:YES
success:^(MMXChannel *channel) {
//Success
} failure:^(NSError *error) {
//Failure
}]
Check Channel Subscription
After getting a channel, check if the current user is subscribed to the channel:
MMXChannel *sfGiantsChannel = ...;
if (sfGiantsChannel.isSubscribed) {
NSLog(@"I'm following the Giants!");
}
Subscribe to Channel
To subscribe to a channel and send and receive messages (sfGiants
):
MMXChannel *sfGiantsChannel = ...;
[sfGiantsChannel subscribeWithSuccess:^{
// do something
} failure:^(NSError *error) {
// do something
}];
Unsubscribe from Channel
To unsubscribe from a channel (in this case, the sfSeals
):
MMXChannel *sfSealsChannel = ...;
[sfSealsChannel unSubscribeWithSuccess:^{
// do something
} failure:^(NSError *error) {
// do something
}];
View Channel Subscribers
After subscribing, you can see other subscribers to the channel (sfGiants
):
MMXChannel *sfGiantsChannel = ...;
[sfGiantsChannel subscribersWithLimit:100 offset:0 success:^(int totalCount, NSArray *subscribers) {
// do something
} failure:^(NSError *error) {
// do something
}];
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).
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
}];
Publish Message to Channel
To publish a message to the channel:
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);
}];
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.
- (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];
}
Once incoming message notification has been enabled, you can continue to implement your notification code as shown below:
- (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];
}
Rename Channel
Additionally, a channel can be renamed. To rename a channel, call the rename
method on the channel object.
MMXChannel *channel;
[channel rename:@"new_name"
success: ^(MMXChannel *channel){
//Channel renaming succeeded!
} failure: ^(NSError *error) {
//Channel renaming failed
}];
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.