super-hero/assets/cc-common/cc-util/UmNativeBridge.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-04-19 03:49:41 -07:00
import * as cc from 'cc';
import { _decorator, Component } from 'cc';
import { UmDeviceInfo } from './UmDeviceInfo';
import { UmClientEvent } from './UmOneToMultiListener';
const { ccclass, property } = _decorator;
@ccclass('UmNativeBridge')
export class UmNativeBridge extends Component {
static _instance: UmNativeBridge;
static get instance() {
if (UmNativeBridge._instance) {
return UmNativeBridge._instance;
}
UmNativeBridge._instance = new UmNativeBridge();
return UmNativeBridge._instance;
}
public vibrate() {
//check web desktop
if (UmDeviceInfo.isDesktopBrowser)
return;
//check mobile browser (ios can not vibrate in browser)
if (UmDeviceInfo.isMobileBrowser && !UmDeviceInfo.isIos) {
navigator?.vibrate([500]);
return;
}
//check native
if (!UmDeviceInfo.isNative)
return;
//android native
if (UmDeviceInfo.isAndroid) {
cc.native.reflection.callStaticMethod("com/cocos/game/AppActivity", "vibrate", "(I)V", 600);
return;
}
//ios native
if (UmDeviceInfo.isIos) {
cc.native.reflection.callStaticMethod("NativeBridge", "VibrateWithIntensity:", "0.5");
return;
}
}
public getTokenFromDeepLink(): string {
if (UmDeviceInfo.isAndroidNative) {
return cc.native.reflection.callStaticMethod("com/cocos/game/AppActivity", "getTokenFromDeepLink", "()Ljava/lang/String;");
}
if (UmDeviceInfo.isIosNative) {
return cc.native.reflection.callStaticMethod("NativeBridge", "GetTokenFromDeepLink:", "0");
}
return "";
}
public onCallNative(evt: string, content: string = "") {
if (UmDeviceInfo.isAndroidNative) {
return cc.native.reflection.callStaticMethod("com/cocos/game/AppActivity", "onCallFromJavascript", "(Ljava/lang/String;Ljava/lang/String;)V", evt, content);
}
// if (UmDeviceInfo.isIosNative) {
// return cc.native.reflection.callStaticMethod("NativeBridge", "GetTokenFromDeepLink:", "0");
// }
}
public onNativeCallJS(evt: string, params: string)
{
UmClientEvent.dispatchEvent(evt, params);
}
}
window.NativeCallJS = (evt: string, params: string) => {
console.log('NativeCallJS------------------------> EVT ' + typeof evt + ' = ' + evt + " ==>> " + params);
UmNativeBridge.instance.onNativeCallJS(evt, params);
}