我想和解析和Pubnub私人聊天。 当用户收到另一位朋友的图像时,他可以点击“通过消息回复”,打开一个新视图,这里是两个朋友之间的私人聊天。 我在框架中使用“BubbleView”来给出iOS消息传递方面。 如何在Pubnub中创建私人频道? 我已经添加解析+ PubNub:私人聊天
PFUser *user = [PFUser currentUser];
channel = [PNChannel channelWithName:user.objectId];
但只影响了渠道谁正在使用的应用程序的用户,而不是渠道为2人...? 用我的代码,我可以收到我自己的消息,控制台说: PubNub(xxxxxxxxxx)订阅频道:( “PNChannel(xxxxxxxxx)objectID(用户从解析谁使用的应用程序)” 消息收到:消息我“已传送
这里是我的代码:
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
和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);
}
您可以在包含两个用户的解析中创建一个聊天对象。然后你可以使用该聊天的对象ID作为pubnub频道。通过这种方式,您可以查询所有用户聊天记录,并了解要监听的频道。如果其他用户开始聊天并且该用户还没有得到通知,这可能很有用。不知道这是否有帮助。 – Logan 2014-12-02 18:33:02
@Logan不错的主意,但如何创建一个包含两个用户的解析聊天对象? – Viny76 2014-12-02 18:44:55
你可以只创建新的对象,如'PFObject *聊天= [PFObject objectWithClassName:@ “聊天”]'然后添加用户:'聊天[@ “用户”] = @ [PFUser currentUser],otherUser]'。一旦设置好了,你可以做一个查询,比如'query whereKey:@“users”equalTo:[PFUser currentUser]];'它将获得当前用户所在的所有聊天记录。 – Logan 2014-12-02 18:50:29