java - PubNub hereNow 方法未返回预期的 uuids 列表

标签 java android pubnub

我在 android 应用程序中使用 PUBNUB api

我是这样设置uuid的

mPubnub.setUUID("customName");

在用户应用中

在客户端应用程序中,我在存在回调中调用 herenow 方法

mPubnub.hereNow("Svnchannel", hereNowCallback);

但是我得到了这样不正确的 uuid 列表

"uuids":["80e3b23f-bad1-4b48-8e89-61c234400d25","50b5c464-cda7-49b8-8ab6-a84ec5de42a1","0465c47b-c03b-4c86-91c4-60ea7267f467"]}

我期待的是这样的

"uuids":["customName1","customName2","customName3"]}

我做错了什么?请帮忙

最佳答案

使用 PubNub Android SDK 设置和重用 UUID

您需要在实例化 Pubnub 对象之后和订阅之前设置 setUUID(String uuid)。这是此代码的简短版本:

Pubnub pubnub = new Pubnub("<your_pub_key>", "<your_sub_key>");
pubnub.setUUID("1234-5678-9ABC-DEFG-HIJK");

最好将此 UUID 缓存在设备上,以便在每次实例化 Pubnub 对象时重用它。或者,您可能会在用户登录到您的服务器后从数据库中的用户配置文件记录中获取用户的 UUID。

这里有一个更完整的生成/缓存/设置/重用 UUID 的解决方案。此代码可能需要一些改进/更新。

// creating the Pubnub connection object with minimal args
Pubnub pubnub = new Pubnub("<your_pub_key>", "<your_sub_key>");

// get the SharedPreferences object using private mode 
// so that this uuid is only used/updated by this app SharedPreferences
sharedPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);

// get the current pn_uuid value (first time, it will be null)
String uuid = getResources().getString(R.string.pn_uuid);

// if uuid hasn’t been created/ persisted, then create
// and persist to use for subsequent app loads/connections 
if (uuid == null || uuid.length == 0) {
    // PubNub provides a uuid generator method but you could 
    // use your own custom uuid, if required
    uuid = pubnub.uuid();
    SharedPreferences.Editor editor = sharedPrefs.edit();    
    editor.putString(getString(R.string.pn_uuid), uuid);
    editor.commit();
}

// set the uuid for the pubnub object
pubnub.setUUID(uuid);

您应该为每个设备使用不同的 UUID。

关于java - PubNub hereNow 方法未返回预期的 uuids 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34577850/

相关文章:

ios - 如何关闭/减少 PubNub iOS SDK 4.0 的控制台输出?

Java 8过滤器但返回所有对象

java - JACOB 和 64 位 JVM - 它有效吗?

javascript - 如何使用pubnub timetoken处理页面刷新

android - 如何在 Android 的 countDownTimer 中使用 Interval

android - 2.0 模拟器之间的 SMS - 当消息长度超过 160 个字符时文本错误?

swift - 使用未声明的类型 PNObjectEventListener

java - 尽管库包含在 pom 中,但在 Maven 项目中找不到类

java - 为什么 Java 日期 API(java.util.Date、.Calendar)如此困惑?

android - "GET PROCESSING OPTIONS"总是6700(错误的Lc或Le)