我想和解析和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