Backendless SDK JS, Messaging

Greetings.
I have a problem with backendless sdk js.
I am using Draftbit to create a mobile app. In this app there is a chat room. I create it with CustomCode using backendless library.

import React, { useState, useEffect } from 'react';
import Backendless from 'backendless';
import * as GlobalVariables from '../config/GlobalVariableContext';
import { View } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';

export const component = () => {
  const Constants = GlobalVariables.useValues();
  const APP_ID = 'key';
  const API_KEY = 'key';
  Backendless.initApp(APP_ID, API_KEY);
  Backendless.UserService.setCurrentUserToken(Constants['USER_TOKEN']);

  const CHANNEL_NAME = 'ChatChannel';

  const [messages, setMessages] = useState([]);

  const channel = Backendless.Messaging.subscribe(CHANNEL_NAME);

  function onMessage(message) {
    setMessages(previousMessages =>
      GiftedChat.append(previousMessages, message.message)
    );
  }
  channel.addMessageListener(onMessage);

  const onSend = async newMessages => {
    try {
      const updatedMessages = GiftedChat.append(messages, newMessages);

      Backendless.Messaging.publish(CHANNEL_NAME, newMessages[0])
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.error(error);
        });

      setMessages(updatedMessages);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <View style={{ flex: 1 }}>
      <GiftedChat
        messages={messages}
        onSend={newMessages => onSend(newMessages)}
        user={{
          _id: Constants['objectId'],
        }}
      />
    </View>
  );
};

In the Backendless project, I have disabled the ability for unauthorized users to send messages. In order to send as from an authorized user I use this method

Backendless.UserService.setCurrentUserToken(Constants['USER_TOKEN']);

I pass the global variable USER_TOKEN from Draftbit into it. On the web version everything works, messages are sent.
But if I use iOS and Android emulators, I get the error

Error: "User has no permission to publish message" in construct@[native code] << construct@[native code] << construct@[native code] << n << construct@[native code]

This error should only occur if the user is not authorized. But it is displayed in any case on iOS and Android.
I don’t understand what I’m doing wrong.

Hi @Rich

try to replace

Backendless.UserService.setCurrentUserToken(Constants['USER_TOKEN']);

with

Backendless.UserService.setCurrentUser({ objectId:'xxx-xxx-xxx', 'user-token': Constants['USER_TOKEN'] })

Regards,
Vlad

1 Like

Hi, Vladimir.
Thank you very much! That helped.