【问题标题】:ParseInstallation.getCurrentInstallation().getObjectId() returns null in the particular user's smartphoneParseInstallation.getCurrentInstallation().getObjectId() 在特定用户的智能手机中返回 null
【发布时间】:2019-07-11 11:43:27
【问题描述】:

我的 Android 应用程序使用 Parse 服务器作为后端服务。 许多用户目前正在使用我的 Android 应用程序。 但是,一些特定用户在安装应用程序时遇到了即使登录后也没有在安装表中创建对象的问题。

当我使用一些分析代码从我的 Android 应用程序中查看问题时,我发现 ParseInstallation.getCurrentInstallation().getObjectId() 为用户返回 null,即使 Parse 初始化过程已正确完成。 使用 Analytics 代码,我在 Parse 初始化后的 10 分钟内每 10 秒检查一次方法,但该方法总是为特定用户返回 null。

// Parse initialization in the onCreate() of Application class
Parse.enableLocalDatastore(this);
Parse.initialize(new Parse.Configuration.Builder(this)
        .applicationId(getString(R.string.parse_app_id))
        .clientKey(getString(R.string.parse_client_key))
        .server(getString(R.string.parse_server_api))
        .build());
ParseUser.enableAutomaticUser();
ParseACL.setDefaultACL(new ParseACL(), true);

// After Log-In, Save the installation.
// (My app user normally would come here 20 ~ 30 seconds or long after the above Parse initialization.) 
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
if (!TextUtils.isEmpty(installation.getObjectId()) { // installation.getObjectId() returns null for the particular user.
    installation.put("GCMSenderId", getString(R.string.fcm_sender_id));
    installation.saveInBackground(
        // will associate the Installation with the User in new SaveCallback()
    );
}

// build.gradle(Module: app)
implementation 'com.github.parse-community.Parse-SDK-Android:parse:1.18.5' 
implementation 'com.github.parse-community.Parse-SDK-Android:fcm:1.18.5'

大多数用户获得有效的 ObjectId 并正常工作,没有任何问题。 但是,一旦用户遇到问题,即使用户再次安装应用程序,也不再在安装表中为用户创建该对象。

我不知道这是否是 Parse 服务器的根本问题。

任何建议都会对我有所帮助。

【问题讨论】:

  • 你能分享installation.saveInBackground(...);的完整代码吗?
  • @Davi Macêdo installation.saveInBackground(...); 无法完成,因为 installation.getObjectId() 返回 null。
  • 我没听懂。如果installation.getObjectId() 为空,TextUtils.isEmpty(installation.getObjectId()) 为真,所以程序进入 if 并执行installation.saveInBackground(...);。不是吗?
  • @Davi Macêdo 我在代码中弄错了。我修改了上面的示例代码。

标签: android parse-platform parse-server parse-android-sdk


【解决方案1】:

我认为 objectId 为空,因为由于某种原因没有保存安装。我建议您使用以下代码并再次检查:

// Parse initialization in the onCreate() of Application class
Parse.enableLocalDatastore(this);
Parse.initialize(new Parse.Configuration.Builder(this)
        .applicationId(getString(R.string.parse_app_id))
        .clientKey(getString(R.string.parse_client_key))
        .server(getString(R.string.parse_server_api))
        .build());
ParseUser.enableAutomaticUser();
ParseACL.setDefaultACL(new ParseACL(), true);

// After Log-In, Save the installation.
// (My app user normally would come here 20 ~ 30 seconds or long after the above Parse initialization.) 
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
if (TextUtils.isEmpty(installation.getObjectId())) { // installation.getObjectId() returns null for the particular user.
    installation.save();
}
installation.put("GCMSenderId", getString(R.string.fcm_sender_id));
installation.saveInBackground(
    // will associate the Installation with the User in new SaveCallback()
);

// build.gradle(Module: app)
implementation 'com.github.parse-community.Parse-SDK-Android:parse:1.18.5' 
implementation 'com.github.parse-community.Parse-SDK-Android:fcm:1.18.5'

【讨论】:

  • 在我的智能手机中重现installation.getObjectId()返回null的情况并不容易。确定调用installation.save()后,objectId、deviceToken和tokenType在Installation表中会有一个有效的字符串值吗?
  • 应该有。您的应用程序中是否还有其他代码可以保存安装?
  • @Davi Macêdo 不。我认为保存方法应该只在一段代码中处理,因为重复可能会导致保存安装的冲突。
  • 我认为你对这段代码很好。做一些测试并检查一下。
  • 我遵循了您的建议,但我找到了遇到问题的特定用户。在installation.save() 之后,installation.getObjectId() 返回一个有效的字符串值。然而,不幸的是,installation.get("deviceToken")installation.get("pushType") 仍然返回 null。使用 Analytics 代码,我在installation.save() 之后的 6 分钟内每 5 秒检查一次 deviceToken 和 pushType,但始终返回 null。
【解决方案2】:

以下是使用 Davi Macêdo 提示的结果:

installation.save() 之后,有时installation.get("deviceToken") 返回空值。 所以我使用了一个迭代例程来等待installation.get("deviceToken") 返回一个有效值。然后我打电话给installation.saveInBackground()。 在这 10 天里,我一直在观察我的应用用户的结果,到目前为止它似乎运行良好。

如果您在电池供电的设备(例如 Android 手机)上使用 Parse Client,您将面临与我类似的问题。 最重要的一点是,当手机处于省电模式时,installation.get("deviceToken") 更频繁地返回 null。 所以在拨打installation.save()installation.saveInBackground()之前,你需要确保手机没有处于省电模式并且网络连接良好。

顺便说一句,在 Davi 的帮助下,我解决了这个问题。感谢戴维。

如果还有其他问题,我会和你分享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多