The AdMob really is great and easy to use for everyone!
But there are some catches, if you are working on a final product you will face some odd problems, if your skill isn’t enough you will be in trouble!
Another issue I’ve found is that the AdMob plugin will overwrite native.bridge.onNative
. It will cuz your incoming actions from native are never triggered! Also, you are supposed to send your data from native to script as JSON forma otherwise an error will be thrown.
Here is the modified version of Bridge.ts
to fix the issue
type onNativeType = (arg0: string, arg1?: string | null)=>void;
const module = "[Bridge]";
export class Bridge {
private OriginalOnNative:onNativeType=null;
init(): Bridge {
log(module, "init");
this.overwriteCallback();
const engineVersion = `cocos-${AdMobVersion}`;
console.log(module, "init", `report engineVersion: ${engineVersion}.`);
this.sendToNative(js.getClassName(VersionREQ), new VersionREQ('', engineVersion), null, null);
return this;
}
destroy() {
log(module, "destroy");
}
overwriteCallback() {
log(module, "overwriteCallback");
if (NATIVE) {
this.OriginalOnNative = native.bridge.onNative;
native.bridge.onNative = this.onNative;
}
}
AdMobonNative = (arg0: string, arg1: string) => {
log(module, `onNative method: ${arg0} | content: ${arg1}`,);
//te.instance.dispatch(arg0, Route.instance.codec.decode(arg1));
const ack = route.codec.decode<Base>(arg1)
route.dispatch(arg0, ack);
}
onNative = (arg0: string, arg1: string) => {
this.OriginalOnNative(arg0,arg1);
this.AdMobonNative(arg0,arg1);
}
sendToNative<TProto extends Base>(arg0: string, req: TProto, responseMethod?: string, onResponse?: INativeResponse, thisArg?: any) {
log(module, "sendToNative", `method = ${arg0}, req.unitId = ${req.unitId}`);
if (onResponse) {
route.once(responseMethod, onResponse, thisArg);
}
if (NATIVE) {
native.bridge.sendToNative(arg0, route.codec.encode(req));
}
}
}
And for JSON errors, you need to change Codec.ts
as follows
export interface ICodec{
decode<T>(content:string) : T
encode<T>(t:T):string
}
export class Codec implements ICodec{
decode<T>(content:string) : T{
try {
let json = JSON.parse(content);
if(json)
return json as T;
} catch (error) {
return content as T;
}
}
encode<T>(t:T):string{
return JSON.stringify(t);
}
}
Change overwriteCallback
function in Bridge.java
as follows
private void overwriteCallback() {
Log.d(TAG, "overwriteCallback: ");
//==========
//IMPORTANT JsbBridge.callback is private and you need to make it public
//==========
JsbBridge.ICallback callback = JsbBridge.callback;
JsbBridge.setCallback(new JsbBridge.ICallback() {
@Override
public void onScript(String arg0, String arg1) {
Log.d(TAG, "onScript: " + arg0 + " | " + arg1);
route.dispatch(arg0, arg1);
callback.onScript(arg0,arg1);
}
});
}