pubnub-chat objective c的用户列表

时间:2016-04-18 11:38:03

标签: ios objective-c ios9 chat pubnub

我有使用pubnub聊天功能的应用程序。 我想执行以下功能

1)register / login user in pubnub programatically for chat
2)Get list of the all the users
3)Make friend and send 1st message to the user

我知道如何创建频道。我通过以下代码创建了频道:

    PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"pub-c-XXXXXXXXXXXX-a2bf-XXXX-XXXX-XXXXXXXXXXXX"subscribeKey:@"sub-c-XXXXXXXXXXXX-02d0-XXXX-XXXX-XXXXXXXXXXXX"];
        self.client = [PubNub clientWithConfiguration:configuration];
        //[self.client addListener:self];
        [self.client subscribeToChannels: @[@"my_channel"] withPresence:YES];


I get the channel architecture by : http://pubnub.github.io/pubnub-design-patterns/2015/03/05/Inbound-Channel-Pattern.html
But how do i get the list of the users and their channel and send the message.


I  also found this : https://www.pubnub.com/docs/ios-objective-c/presence-sdk-v4
but this only show the friends status. whether they are online / offline by 

    Join
    leave 
    timeout

Please advice and help

查找用户列表的代码如下,但仍显示ZERO用户:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"pub-c-XXXXXXXXXXXX-a2bf-XXXX-XXXX-XXXXXXXXXXXX"subscribeKey:@"sub-c-XXXXXXXXXXXX-02d0-XXXX-XXXX-XXXXXXXXXXXX"];
        self.client = [PubNub clientWithConfiguration:configuration];
        //Subscription process results arrive to listener which should adopt to PNObjectEventListener protocol and registered using:
        [self.client addListener:self];

        //Listeners callbacks:
        [self.client subscribeToChannels: @[@"XXX"] withPresence:YES];
        configuration.uuid = @"XXX";
        [self.client hereNowForChannel:@"XXX" withVerbosity:PNHereNowState
                            completion:^(PNPresenceChannelHereNowResult *result,
                                         PNErrorStatus *status) {

                                // Check whether request successfully completed or not.
                                if (!status.isError) {
                                    NSLog(@"list of users %@", result.data.uuids);
                                    NSLog(@"list of result.data.occupancy %@", result.data.occupancy);

                                    //   result.data.uuids - dictionary with active subscriber. Each entry will have next
                                    //                       fields: "uuid" - identifier and "state" if it has been provided.
                                    //   result.data.occupancy - total number of active subscribers.
                                }
                                // Request processing failed.
                                else {
                                    NSLog(@"FAIL");
                                    // Handle presence audit error. Check 'category' property to find
                                    // out possible issue because of which request did fail.
                                    //
                                    // Request can be resent using: [status retry];
                                }
                            }];

    }

2 个答案:

答案 0 :(得分:1)

PubNub现在使用状态

您需要使用hereNowForChannel致电withVerbosity:PNHereNowState以获取用户的状态以及所有订阅的用户。

Objective-C SDK API Reference Docs for hereNowForChannel有更多详情。

答案 1 :(得分:1)

查看代码,如果您始终将UUID设置为@" XXX"你将只有1个用户。 PubNub的Presence插件使用UUID属性来跟踪频道上的活动用户,如果您只使用1个UUID,PubNub会将每个客户端视为同一个客户端。

以下是有关设置UUID的知识库文章 https://www.pubnub.com/knowledge-base/discussion/138/how-do-i-set-the-uuid

此外,iOS PubNub SDK将自动创建和重用UUID。这是4.2 sdk的新功能。 https://www.pubnub.com/docs/ios-objective-c/api-reference-sdk-v4#uuid_example_2