【问题标题】:Publishing messages from Parse via PubNub通过 PubNub 从 Parse 发布消息
【发布时间】:2014-05-07 22:32:50
【问题描述】:

我想将 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 中存储的 PFObjects 的 url?

【问题讨论】:

    标签: objective-c parse-platform pubnub


    【解决方案1】:

    解析 + PubNub 发布消息

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

    在 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。您可以改为在用户之间直接向每个用户的频道名称发送消息。 Sally 有"channel-sally",Bill 有"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 或他的任何其他朋友发送消息。

    【讨论】:

    • 如何将用户分配到频道?我的意思是,我怎样才能实现该账单的渠道是渠道账单?例如,我可以使用类似的东西来定义我的频道:PFUser *currentUser = [PFUser currentUser]; PNChannel *my_channel = [PNChannel channelWithName:@"currentUser"]; ?
    • @sabin 好问题。您可以通过从 Parse 获取USERID 并使用USERID 作为频道名称来分配用户的频道。
    • 太好了,我会尝试这样做。非常感谢您的帮助,谢谢。直接从源头获取信息真是太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多