【问题标题】:PubNub hereNow method not returning the expected uuids listPubNub hereNow 方法未返回预期的 uuids 列表
【发布时间】:2016-01-03 14:54:30
【问题描述】:

我在 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,只是看看docs。似乎对setUUID() 的调用将检查传递的字符串是否与设备上生成的唯一ID 相关;如果是,则返回之前创建的唯一 ID,否则将生成一个新 ID。
  • 扩展我之前的评论:this article 解释了如何在每次使用应用程序时创建和重用 UUID。 HTH!
  • 订阅前需要设置mPubnub.setUUID("customName123")
  • stackoverflow.com/users/2779561/saveen 在您订阅之前设置 UUID 是否解决了您的问题?

标签: java android pubnub


【解决方案1】:

使用 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。

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2017-10-21
    • 1970-01-01
    • 2018-10-21
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多