(2)、添加Firebase SDK
A、在项目根目录 (Project) build.gradle 中添加如下内容:
dependencies { classpath 'com.google.gms:google-services:4.3.8' // ... }
B、在应用级(app/build.gradle)中,添加如下内容:
apply plugin: 'com.google.gms.google-services' dependencies { implementation platform('com.google.firebase:firebase-bom:28.3.0') }
(3)、添加 Google Analytics(事件上报及分析)
A、在应用级(app/build.gradle)中,添加如下内容:
implementation 'com.google.firebase:firebase-analytics'
B、 添加网络权限,已添加则忽略。
<uses-permission android:name="android.permission.INTERNET" />
C、java 封装事件上报接口,支持注册、登录、充值事件
public static void sendFireBaseEvent(final String a_name,final String a_json){ Log.d(TAG, "sendFireBaseEvent:" + a_name + ":"+ a_json); if (TextUtils.isEmpty(a_name)) { Log.e(TAG, "sendFireBaseEvent. a_name isnull "); return; } final Bundle params = new Bundle(); if (!TextUtils.isEmpty(a_json)) { try { JSONObject jsonObj = new JSONObject(a_json); if(a_name.equals(FirebaseAnalytics.Event.PURCHASE)){ String uid = jsonObj.get("af_content_id").toString(); double amount = jsonObj.getDouble("af_revenue"); String currency = jsonObj.get("af_currency").toString(); String content_type = jsonObj.get("af_content_type").toString(); params.putString("uid", uid); params.putDouble(FirebaseAnalytics.Param.VALUE, amount); params.putString(FirebaseAnalytics.Param.CURRENCY, currency); params.putString(FirebaseAnalytics.Param.CONTENT_TYPE, content_type); } } catch (JSONException e) { e.printStackTrace(); } } FirebaseAnalytics.getInstance(Application.getContext()).logEvent(a_name, params); }
D、ts 或js 代码封装 java 接口
export class Native { private static ApiAndroid: string = "org/cocos2dx/javascript/Native"; private static ApiIOS: string = "AppController"; public static sendFireBaseEvent(eventName: string, jsonStr: string = ""): string { // cc.log("sendAFEvent",eventName,JSON.stringify(jsonStr)) let ret = ""; if (cc.sys.isBrowser) { } else if (cc.sys.isNative) { if (cc.sys.os == cc.sys.OS_ANDROID) { ret = jsb.reflection.callStaticMethod(Native.ApiAndroid, "sendFireBaseEvent", "(Ljava/lang/String;Ljava/lang/String;)V", eventName, jsonStr); } else if (cc.sys.os == cc.sys.OS_IOS) { // ret = jsb.reflection.callStaticMethod(Native.ApiIOS, "sendFireBaseEvent", null); } } return ret; } }
E、ts 或js 代码中,使用注册、登录、充值事件上报
export enum FireBase_EVENT_ENUM { LOGIN = "login", REGIST = "sign_up", PURCHASE = "purchase", } Native.sendFireBaseEvent(FireBase_EVENT_ENUM.REGIST) Native.sendFireBaseEvent(FireBase_EVENT_ENUM.LOGIN) sendRechargeEvent(a_gold: number) { let afEventJson = JSON.stringify({ "af_revenue": a_gold, "af_content_type": "web_pay", "af_content_id": PlayerInfo.get("dwUserID"), "af_currency": "INR", }) console.log("上报充值事件", afEventJson) Native.sendFireBaseEvent(FireBase_EVENT_ENUM.PURCHASE, afEventJson) }
(3)、DebugView模式下测试
为什么要采用DebugView模式下测试?
这是因为正常事件上报有一定的延迟,不能进行实时反馈,采用DebugView 可以实时展示上报事件在控制台界面上。
首先,启动调试模式
adb shell setprop debug.firebase.analytics.app com.example.test
然后,在控制台开启DebugView
如果接入ok,DebugView界面左上角会显示测试手机的型号:
如上图所示,DebugView模式下有数据,则说明 Firebase sdk接入就没什么问题。
如果没有显示手机型号可尝试以下操作:
- 检测启动调试模式的adb命令是否正确
- 检查测试设备是否开启了USB调试
- 检查测试设备是否能访问google相关网站(科学上网)
- 检查目标设备是否安装 google 服务
至此, Cocos Creator Android 平台接入 Google Firebase (Analytics功能) 流程结束。