Mobile Backend Starter subscribeToCloudMessage will not work

后端 未结 4 1411
情书的邮戳
情书的邮戳 2020-12-10 22:15

When I use the subscribeToCloudMessage() function with CloudBackendMessaging.TOPIC_ID_BROADCAST as the topicId as is done in CloudBackendFragment.java everything works fine

4条回答
  •  感情败类
    2020-12-10 22:42

    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.

    From the docs:

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

    1. Short strings (up to 500 Unicode characters or bytes) are indexed and can be used in query filter conditions and sort orders.
    2. Longstrings (up to 1 megabyte) are not indexed and cannot be used in query filters and sort orders.

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

    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:

    To set Text property replace this line of code:

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

    with this line:

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

    To get Text property from the datastore:

    1. Replace this line:

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

      with this:

      Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
          String subscriptionString = text.getValue();
      
    2. Replace this line:

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

      with this:

      Text text = (Text) deviceSubscription.getProperty(PROPERTY_SUBSCRIPTION_IDS);
            String ids = text.getValue();
      
    3. Replace this line:

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

      with this:

      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.

    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.

提交回复
热议问题