Call client methods from a card

更新时间:
复制 MD 格式

Custom JSAPI

Android

public class CustomCubeModule extends CubeModule {
    private static final String TAG = CustomCubeModule.class.getSimpleName();

    // Annotation. uiThread indicates whether the callback is in the main process.
    @JsMethod(uiThread = true)
    public void cubeToClient(final CubeJSCallback callback) { 
        // Send a callback to the card.
        if (callback != null) {
            callback.invoke("cubeToClient callback data: " + System.currentTimeMillis());
        }
    }
}

iOS

  1. In the project, create an NSObject object and import the <CubeModuleProtocol> protocol.

    #import <Foundation/Foundation.h>
    #import <CubeCrystal/CubeModuleProtocol.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MPCubeModule : NSObject <CubeModuleProtocol>
    
    @end
    
    NS_ASSUME_NONNULL_END
  2. Implement the method based on the template convention.

    #import "MPCubeModule.h"
    #import <CubeBridge/CKJSDefine.h>
    
    @implementation MPCubeModule
    
    // cardUid is a property of the module. When the module API is called, cardUid identifies which card initiated the call. This cardUid is the same as the cardUid in CubeCard.
    @synthesize cardUid;
    
    // Configure the conventional method.
    CK_EXPORT_METHOD(@selector(cubeToClient:))
    
    - (void)cubeToClient:(CubeModuleMethodCallback)callback {
        // Execute the cubeToClient operation here.
        if (callback) {
            // The parameter to be passed back to the template.
            callback(@"cubeToClient");
        }
    }
    
    @end

HarmonyOS

export class NativeCubeModule implements IFalconModule {
  /**
   * Used for card calls.
   * @returns
   */
  getName(): string {
    return "custom";
  }

  /**
   * The method called by the card.
   * @param instance
   * @param params
   * @param callback
   */
  cubeToClient(instance: FalconInstance, params: object, callback: IFalconCallback) {
    hilog.debug(0x0000, `cubeToClient: ${params}`, '');
    promptAction.showToast({
      message: JSON.stringify(params)
    })
    callback.invoke("cubeToClient callback data: " + systemDateTime.getTime())
  }
}

Register the JSAPI

Android

The first parameter specifies the type, the second specifies the full path of the custom module, and the third specifies the method name. Do not obfuscate the full path or the method name.

Collection<CubeModuleModel> cubeModuleModels = new LinkedList<>();
cubeModuleModels.add(new CubeModuleModel("custom", CustomCubeModel.class.getName(), new String[]{"cubeToClient"}));
CubeService.instance().getEngine().registerModule(cubeModuleModels, null);

iOS

- (void)registerModules {
    [MPCubeManager shareManager];
    
    // Register the custom module. The key is the module name (by convention with the server-side), and the value is the client class name.
    NSDictionary *dic = @{@"custom": @"MPCubeModule"};
    [[[CubeService sharedInstance] getEngine] registerModules:dic];
}

HarmonyOS

CubeEngine.getInstance().registerModule(new NativeCubeModule())

Call the JSAPI

<script>
    // Register the module.
    const navigator = requireModule("custom");
    export default {
        data: {
            message: 'Hello Cube 1'
        },
        beforeCreate() {
            this.message = 'Hello Cube 2'
        },
        didAppear() {
            
        },        
        methods: {
            onClick() {
                // Call the client method.
                navigator.cubeToClient(event=>{
                    this.message = event;
                })
            }
        }
    }
</script>