通过PubNub从Parse发布消息

时间:2014-05-07 22:32:50

标签: objective-c parse-platform pubnub

我想将PubNub与Parse一起用于聊天模块。有人可以解释我如何通过PubNub向用户发送带有文本和图像的消息(只有一对一)?我想使用PFUser用户名作为用户私有频道的ID。

我在PubNub help docs中找到了这段代码,但我从来没有在objective-c中看到这样的代码。我应该在哪里/如何使用这条线?

curl https://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<CHANNEL-NAME>/0/%22Hellooooooo%22

我也不清楚我应该在哪里存储消息?在Parse还是我可以只在PubNub存储它们?我不确定第二种变化是否可行,因为我没有在PubNub上看到任何数据存储。或者例如我只发送我在Parse存储的PFObject的网址?

1 个答案:

答案 0 :(得分:10)

Parse + PubNub发布消息

PubNub Parse

您可以将解析云内的消息发布给您的移动用户,以便轻松创建聊天体验。如果要直接从Parse Cloud中发送消息,请使用以下代码向用户发送消息:

在解析云上发布PubNub消息

Parse.Cloud.httpRequest({
  url: 'https://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<USER-CHANNEL-NAME>/0/%22Hellooooooo!%22',
  // successful HTTP status code
  success: function(httpResponse) {
    console.log(httpResponse.text);
  },
  // unsuccessful HTTP status code
  error: function(httpResponse) {
    console.error('Request failed with response code ' + httpResponse.status);
  }
});

您不一定需要以这种方式使用Parse。您可以直接在用户之间向每个用户的频道名称发送消息。莎莉有"channel-sally",比尔有"channel-bill"。您可以直接从Objective-C应用程序代码发布和订阅。 Bill将订阅他自己的频道和Sally到她的频道。

鲍勃的应用

// Define a channel
PNChannel *myChannel     = [PNChannel channelWithName:@"channel-bob"];
PNChannel *friendChannel = [PNChannel channelWithName:@"channel-sally"];

// Receive Messages Sent to Me!
[PubNub subscribeOnChannel:myChannel];

// Send a Message to Sally
[PubNub sendMessage:@"Hello from PubNub iOS!" toChannel:friendChannel];

//(In AppDelegate.m, define didReceiveMessage delegate method:)
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
   NSLog(@"Received: %@", message.message);
}

Bob可以在自己的频道上接收消息,并且可以向Sally或他的任何其他朋友发送消息。

相关问题