【问题标题】:Implementing Azure Communication Services - Chat in Android application实现 Azure 通信服务 - Android 应用程序中的聊天
【发布时间】:2021-08-27 11:06:56
【问题描述】:

我正在尝试使用 Azure 通信服务 (Chat) 在 Android 应用中实现聊天功能。我使用https://docs.microsoft.com/en-us/azure/communication-services/quickstarts/chat/get-started?pivots=programming-language-android这个链接来设置Android项目。在 Android 应用中,我们需要很少的 Azure 通信资源配置数据,例如,

String endpoint = "https://<resource>.communication.azure.com";
String firstUserId = "<first_user_id>";
String secondUserId = "<second_user_id>";
String firstUserAccessToken = "<first_user_access_token>";

通过微软文档,我找到了这个链接Access Token Generation,一个用于创建Identity.ID和Access Token的控制台应用程序。我使用了相同的访问令牌和 Identity.ID,它是由控制台应用程序为 FirstUserAccessToken 和 First UserID 生成的。完成此操作后,我运行了 Android 应用程序,但在调用时出现 Null 指针异常

CreateChatThreadResult createChatThreadResult = chatAsyncClient.createChatThread(createChatThreadOptions).get();

在调用 .get 时应用会抛出异常。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int com.azure.android.core.http.HttpResponse.getStatusCode()”

首先,想知道我使用的AccessToken和UserId是否正确。

我在这里遗漏了什么吗?或者是否需要为此进行任何服务器端设置?

【问题讨论】:

    标签: android azure azure-communication-services


    【解决方案1】:

    首先,想知道我使用的AccessToken和UserId是否正确。

    是的,使用CommunicationIdentityClient 创建用户并颁发令牌是正确的方法。只要确保您指定了正确的范围。此示例requires 设置CHAT 范围。

    List<CommunicationTokenScope> scopes = Arrays.asList(CommunicationTokenScope.CHAT);
    CommunicationUserIdentifierAndToken result = communicationIdentityClient.createUserAndToken(scopes);
    

    最近,quickly generate Azure Communication Services access tokens for testing 添加了一个新选项 - 您可以直接在 Azure 门户中进行操作。

    我刚刚快速尝试了来自 GitHub 的 finalized sample code,它运行良好。我采取的步骤:

    • 已安装 Android Studio + Android 11 SDK(API 级别 30)
    • 创建了运行 Android 11 的 Pixel 4 设备
    • 克隆了repo
    • 将 endpoint、firstUserId、secondUserId、firstUserAccessToken 替换为 Azure 门户中生成的值
    • 运行示例 + 成功调试

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 2021-08-16
      • 2015-10-03
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 2012-09-24
      • 2022-06-30
      • 2015-08-18
      相关资源
      最近更新 更多