I want to make a private chat with Parse and Pubnub. When a user receives an image from another friend, he can click on "reply by message", that opens a new view, and here is the private chat between the 2 friends. I use "BubbleView" in framework for giving an iOS messaging aspect. How can I make a private channel in Pubnub ? I've added
PFUser *user = [PFUser currentUser];
channel = [PNChannel channelWithName:user.objectId];
but that affects only the channel to the user who are using the app, not channel for the 2 persons ... ? With my code I can receive my own message, the console says : PubNub (xxxxxxxxxx) SUBSCRIBED ON CHANNELS: ( "PNChannel(xxxxxxxxx) objectID(user from parse who are using the app)" Message received: the message i've sent
Here is my code :
ChatViewController.h :
#import "MessagesViewController.h"
#import "PNImports.h"
@interface ChatViewController : MessagesViewController
@property (strong, nonatomic) NSMutableArray *messages;
@end
ChatViewController.m :
#import "ChatViewController.h"
#import <Parse/Parse.h>
@interface ChatViewController ()
@end
PNChannel *channel;
id message;
NSDate *receiveDate;
NSString *text;
@implementation ChatViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Messages";
self.messages = [[NSMutableArray alloc] initWithObjects:
@"Testing some messages here.", @"lol",
nil];
UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[exitButton addTarget:self action:@selector(backToInboxView) forControlEvents:UIControlEventTouchUpInside];
[exitButton setTitle:@"Inbox" forState:UIControlStateNormal];
exitButton.frame = CGRectMake(0.0, 0.0, 60, 60);
[self.view addSubview:exitButton];
// #1 Define client configuration
PNConfiguration *myConfig = [PNConfiguration configurationForOrigin:@"pubsub.pubnub.com"
publishKey:@"demo"
subscribeKey:@"demo"
secretKey:nil];
// #2 make the configuration active
[PubNub setConfiguration:myConfig];
// #3 Connect to the PubNub
[PubNub connect];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.messages.count;
}
#pragma mark - Messages view controller
- (BubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (indexPath.row % 2) ? BubbleMessageStyleIncoming : BubbleMessageStyleOutgoing;
}
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.messages objectAtIndex:indexPath.row];
}
- (void)sendPressed:(UIButton *)sender withText:text
{
[self.messages addObject:text];
if((self.messages.count - 1) % 2)
[MessageSoundEffect playMessageSentSound];
else
[MessageSoundEffect playMessageReceivedSound];
PFUser *user = [PFUser currentUser];
channel = [PNChannel channelWithName:user.objectId];
// Receive Messages Sent to Me!
[PubNub subscribeOnChannel:channel];
// Send a Message to Sally
[PubNub sendMessage:text toChannel:channel];
[self finishSend];
}
- (void)backToInboxView{
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
and in Appdelegate.m :
- (void)pubnubClient:(PubNub *)client didSubscribeOnChannels:(NSArray *)channels {
NSLog(@"DELEGATE: Subscribed to channel:%@", channels);
}
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
NSLog(@"Message received: %@", message.message);
}