通过 PubNub 从 Parse 发布消息 [英] Publishing messages from Parse via PubNub

查看:23
本文介绍了通过 PubNub 从 Parse 发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to use PubNub with Parse for a chat module. Could somebody explain me how can i send messages with text and images via PubNub to a user (only one-to-one)? I wanna use the PFUser usernames as the id of a user's private channel.

我在 PubNub 中找到了此代码 帮助文档,但我从未在objective-c 中看到过这样的代码.我应该在哪里/如何使用这条线?

I've found this code in the PubNub help docs, but i've never seen code like this in objective-c. Where/how should i use this line?

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

我也不清楚应该在哪里存储消息?在 Parse 中还是我只能将它们存储在 PubNub 中?我不确定第二种变体是否可行,因为我在 PubNub 上没有看到任何数据存储.或者例如我只发送我在 Parse 存储的 PFObjects 的 url?

It's also unclear for me that where should i store the messages? In Parse or can i store them only at PubNub? I'm not sure that the second variation is possible, because i didn't see any data storage at PubNub. Or for example i'm sending only the url's of the PFObjects that i store at Parse?

推荐答案

Parse + PubNub 发布消息

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

You can publish messages inside of parse cloud to your mobile users to create chat experiences easily. If you want to send the messages directly from within Parse Cloud use the following code to send a message to a user:

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 将订阅她的频道.

You don't necessarily need to use Parse this way. You can instead send messages directly between users to each user's channel name. Sally has "channel-sally", and Bill has "channel-bill". You can publish and subscribe directly form your Objective-C App Code. Bill will subscribe to his own channel and a Sally to her channel.

// 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 或他的任何其他朋友发送消息.

Bob can receive messages on his own channel and he can send messages to Sally or any other of his friends.

这篇关于通过 PubNub 从 Parse 发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆