Creating a Private Discussion Group
A private discussion group allows you to create an "invite-only" discussion group so that people can discuss a topic privately. The following use case describes how to create and interact with a Company Policies private discussion group.
Create Channel
Start by creating a channel object:
MMXChannel.createWithName(
"CompanyPolicies",
summary: "Stay upto date with the company policies",
isPublic: false,
publishPermissions: .Subscribers,
success: { channel in
}) { error in
}
Discover Private Channels
To discover all the private channels you own:
MMXChannel.allPrivateChannelsWithLimit(10, offset: 0,
success: { totalCount, channels in
}) { error in
}
To discover your private channels by name (e.g., my_private_channel
):
MMXChannel.channelForName("my_private_channel", isPublic: false,
success: { channel in
}) { error in
}
Invite Users to Channel
Invite users to join a channel:
let janeDoe:MMUser = ...
mmxChannel.inviteUser(janeDoe, comments: "Subscribe to the Company Policies channel",
success: { invite in
}) { error in
}
Notifications are leveraged to listen for incoming invites:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveChannelInvite:", name: MMXDidReceiveChannelInviteNotification, object: nil)
}
func didReceiveChannelInvite(notification: NSNotification) {
// Do something with the invite
let userInfo : [NSObject : AnyObject] = notification.userInfo!
let invite = userInfo[MMXInviteKey] as! MMXInvite
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
To display channel information from the invite like name, summary and owner:
let janeDoeInvite: MMXInvite = ...
janeDoeInvite.timestamp // returns the time the invite was sent
janeDoeInvite.comments // returns "Subscribe to the Company Policies channel"
janeDoeInvite.sender // returns inviter
let mmxChannel = janeDoeInvite.channel
mmxChannel.ownerUserID // returns userID for owner of the channel
mmxChannel.name // returns "CompanyPolicies"
mmxChannel.summary // returns "Stay upto date with the company policies"
Accept Invites
To accept an invite:
// Comments are optional, can be nil
janeDoeInvite.acceptWithComments("Thanks", success: {
}) { error in
}
Decline Invites
To decline an invite:
// Comments are optional, can be nil
janeDoeInvite.declineWithComments("Thanks, but I'm transferring to the Berlin office!",
success: {
}) { error in
}
Receive Invitation Responses
To receive invitation responses:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveChannelInviteResponse:", name: MMXDidReceiveChannelInviteResponseNotification, object: nil)
}
func didReceiveChannelInviteResponse(notification: NSNotification) {
let userInfo : [NSObject : AnyObject] = notification.userInfo!
let inviteResponse = userInfo[MMXInviteResponseKey] as! MMXInviteResponse
let message: String?
if inviteResponse.accepted {
message = "accepted"
} else {
message = "declined"
}
print("\(inviteResponse.sender.userName) \(message) invite to join \(inviteResponse.channel.name)")
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
Publish to Channel
Publish to a channel using the code below:
let messageContent = ["message":"Hello channel!"]
myChannel.publish(messageContent, success: { message in
print("Successfully published to (myChannel)")
}) { error in
print("Could publish to \(myChannel)\nError = \(error)")
}
To register for a notification to receive incoming messages from the channel:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Indicate that you are ready to receive messages now!
MMX.start()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "didReceiveMessage:",
name: MMXDidReceiveMessageNotification,
object: nil)
}
Once the incoming message notification has been enabled, you can continue to implement your notification code as shown below:
func didReceiveMessage(notification: NSNotification) {
let userInfo : [NSObject : AnyObject] = notification.userInfo!
let message = userInfo[MMXMessageKey] as! MMXMessage
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(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.
let channel = ...
channel.rename("new_name", success: { channel in
//Channel renaming succeeded!
}, failure: { error in
//Channel renaming failed
})
Useful Links