移动后端启动器 subscribeToCloudMessage 不起作用 [英] Mobile Backend Starter subscribeToCloudMessage will not work

查看:31
本文介绍了移动后端启动器 subscribeToCloudMessage 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 subscribeToCloudMessage() 函数和 CloudBackendMessaging.TOPIC_ID_BROADCAST 作为 topicId 时,就像在 CloudBackendFragment.java 中所做的那样,一切正常,但是当我给这个函数我自己的字符串时,我收到了这条消息:

When I use the subscribeToCloudMessage() function with CloudBackendMessaging.TOPIC_ID_BROADCAST as the topicId as is done in CloudBackendFragment.java everything works fine but when I give this function my own string I get this message:

错误:

m.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

"code": 400,
"errors": [
  {
    "domain": "global",
    "message": "SubscriptionIDs: String properties must be 500 characters or less.  Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length.",
    "reason": "badRequest"
  }
],
"message": "SubscriptionIDs: String properties must be 500 characters or less.  Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length."

at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1042)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.google.cloud.backend.core.CloudBackend.list(CloudBackend.java:314)
at com.google.cloud.backend.core.CloudBackendAsync.access$8(CloudBackendAsync.java:1)
at com.google.cloud.backend.core.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:270)
at com.google.cloud.backend.core.CloudBackendAsync$9.callBackend(CloudBackendAsync.java:1)
at com.google.cloud.backend.core.CloudBackendAsync$BackendCaller.run(CloudBackendAsync.java:402)

推荐答案

此错误消息意味着属性 SubscriptionIDs(您可以在数据存储中的 _DeviceSubscription Kind 下找到它) 值超过 500 个 Unicode 字符限制.阅读文档以获取参考.

This error message means that property SubscriptionIDs (you can find it under the _DeviceSubscription Kind in your datastore) value exceeds 500 Unicode characters limit. Read docs for the reference.

来自文档:

对于文本字符串和未编码的二进制数据(字节字符串),数据存储区支持两种值类型:

For text strings and unencoded binary data (byte strings), the Datastore supports two value types:

  1. 短字符串(最多 500 个 Unicode 字符或字节)被索引并且可以用于查询过滤条件和排序顺序.
  2. 长字符串(最多 1 兆字节)未编入索引,不能用于查询过滤器和排序顺序.

发生这种情况的原因是 MBS 试图将所有订阅写入一个属性.

The reason for this happening is that MBS trying to write all subscriptions into one property.

所以为了克服这个问题,我们需要在 MBS 后端源代码,用于 SubscriptionIDs 属性.为此,您需要在 DeviceSubscription.java 类中进行以下更改:

So to overcome this issue we need to use Text instead of String type in MBS backend source code for the SubscriptionIDs property. To do so you need to make following changes in DeviceSubscription.java class:

设置Text属性替换这行代码:

To set Text property replace this line of code:

deviceSubscription.setProperty(PROPERTY_SUBSCRIPTION_IDS, this.gson.toJson(subscriptions));

用这一行:

deviceSubscription.setProperty(PROPERTY_SUBSCRIPTION_IDS, new Text(this.gson.toJson(subscriptions)));

从数据存储中获取 Text 属性:

To get Text property from the datastore:

  1. 替换这一行:

  1. Replace this line:

String subscriptionString = (String) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);

这样:

Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
    String subscriptionString = text.getValue();

  • 替换这一行:

  • Replace this line:

    String ids = (String) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
    

    这样:

    Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
          String ids = text.getValue();
    

  • 替换这一行:

  • Replace this line:

    String[] ids = new Gson().fromJson((String) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS),
                String[].class);
    

    这样:

    Text text = (Text) entity.getProperty(PROPERTY_SUBSCRIPTION_IDS);
            String[] ids = new Gson().fromJson(text.getValue(), String[].class);
    

  • 似乎是有效的解决方案.到目前为止,我还没有注意到对我的项目的负面影响.

    Seems to be valid solution. I have not noticed negative influence on my project so far.

    注意:正如文档所述,Text 未编入索引,不能用于查询过滤器和排序顺序.因此,如果需要将属性 SubscriptionIDs 编入索引,则此限制可能会导致另一个问题.

    NOTE: As docs stated Text is not indexed and cannot be used in query filters and sort orders. So this limitation could have cause another issues if the property SubscriptionIDs required to be indexed.

    这篇关于移动后端启动器 subscribeToCloudMessage 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆