小专栏地址ios
Firebase是一家实时后端数据库创业公司,它能帮助开发者很快的写出Web端和移动端的应用。自2014年10月Google收购Firebase以来,用户能够在更方便地使用Firebase的同时,结合Google的云服务 地址数据库
由于咱们公司是用户群是在台湾,因此在第三方库的选型上选择了Firebase。由于它拥有较丰富的第三方库集。后端
pod 'Firebase/Core' 复制代码
3.添加Firebase配置文件 将 Firebase 添加到您的 iOS 项目api
[FIRApp configure];
复制代码
pod | 服务 |
---|---|
pod 'Firebase/Core' | 必备库和 Analytics |
pod 'Firebase/AdMob' | AdMob |
pod 'Firebase/Messaging' | 必备库和 Analytics |
pod 'Firebase/AdMob' | AdMob |
pod 'Firebase/Database' | 实时数据库 |
pod 'Firebase/Invites' | 邀请 |
pod 'Firebase/DynamicLinks' | 动态连接 |
pod 'Fabric',pod 'Crashlytics' | Crashlytics |
pod 'Firebase/RemoteConfig' | 远程配置 |
pod 'Firebase/Auth' | 身份验证 |
pod 'Firebase/Storage' | 存储 |
pod 'Firebase/Performance' | 性能监控 |
pod 'Firebase/Firestore' | Cloud Firestore |
pod 'Firebase/Functions' | Cloud Functions for Firebase 客户端 SDK |
pod 'Firebase/MLVision' | ML Kit Vision API |
pod 'Firebase/MLVisionLabelModel' | 机器学习套件(基于设备的标签检测) |
pod 'Firebase/MLVisionBarcodeModel' | 机器学习套件(基于设备的条形码扫描) |
pod 'Firebase/MLVisionTextModel' | 机器学习套件(基于设备的文字识别) |
pod 'Firebase/MLVisionFaceModel' | 机器学习套件(基于设备的面部检测) |
1.配合GTM SDK 将firebase采集到的数据发送到Google Analytics中bash
添加GTM SDK 与 Google Analytics SDKmarkdown
pod 'GoogleIDFASupport' // Google Analytics pod 'GoogleTagManager' // GTM SDK 复制代码
登陆GTM的平台 地址 网络
建立触发器 app
建立代码 GA-ID变量便是 咱们的Google Analytics平台的ID 机器学习
简述:咱们先在 GTM container 网页上设定想追踪的 tag(标籤),每一个 tag 会包含一个 trigger(触发条件),当 Firebase 发送事件后 GTM 会去比对这个事件是否是符合某个 tag 的触发条件,在此次的例子中就会将数据发送到 GA 报表上。触发条件比较灵活,能够自定义。包括维度的划分。这里就再也不详细介绍。函数
firebase的发送函数是
//触发器事件名称 #define Event_Trigger @"event_trigger_str" // 统计类别 #define Event_Category @"event_category_str" // 统计动做 #define Event_Action @"event_action_str" // 统计标签 #define Event_Label @"event_label_str" // 便可将统计事件发送到GA中 + (void)fireBaseEvent:(NSString *)category action:(NSString *)action label:(NSString *)label{ [FIRAnalytics logEventWithName:Event_Param_Trigger parameters:@{Event_Param_Category:category,Event_Param_Action:action,Event_Param_Label:label}]; } 复制代码
经过cocopod库设置Fcm sdk, fcm 中支持通知消息和数据消息
在"专案设置-设置-Cloud Messaging"中上传开发者凭证到fcm平台
配置代码以下
//经过Cocopod导入 pod 'Firebase/Messaging' 复制代码
// 第一步设置代理 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //配置FCM 代理 [FIRMessaging messaging].delegate = self; } - (void)applicationDidEnterBackground:(UIApplication *)application { [self disconnectToFCM]; } - (void)applicationDidBecomeActive:(UIApplication *)application{ [self connectToFCM]; } // 注册TOKEN - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ #if DEBUG [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox]; #else [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeProd]; #endif } #pragma mark - RemoteMessage Connect // 连接fcm - (void)connectToFCM{ NSString *refreshedToken = [[FIRInstanceID instanceID] token]; DLog(@"InstanceID token: %@", refreshedToken); [FIRMessaging messaging].shouldEstablishDirectChannel = YES; } // 断开fcm - (void)disconnectToFCM{ [FIRMessaging messaging].shouldEstablishDirectChannel = NO; } #pragma mark - FIRMessagingRemoteMessage Delegate //获取fcmtoken回调函数 - (void)messaging:(nonnull FIRMessaging *)messaging didReceiveRegistrationToken:(nonnull NSString *)fcmToken{ DLog(@"didReceiveRegistrationToken fcmToken = %@",fcmToken); [self connectToFCM]; // 注意 将fcm 发送到本身的后端采集token及用户的设备id,用于发送推送 // ....... } //注意:fcm data 数据消息的接收回调,若是是通知消息回调,走的是Apple 的api此处不详述 //ios10 以前didReceiveRegistrationToken - (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage{ DLog(@"RemoteMessageDeleagte remoteMessage = %@",remoteMessage); } //iOS10 及以后 - (void)messaging:(nonnull FIRMessaging *)messaging didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage{ DLog(@"RemoteMessageDeleagte didReceiveMessage = %@",remoteMessage);//fcm data 数据没有带前缀gcm.notification. } 复制代码
//经过Cocopod导入 pod 'Firebase/Performance' 复制代码
performance 只要解决导入库,便可。默认会在[FIRApp configure]; 已初始化
// 自定义采集是经过setValue:forAttribute 方式设置采集属性及值 + (void)performaceLoadTimeWithName:(NSString *)name comeDate:(NSDate *)comeDate{ FIRTrace *trace = [FIRPerformance startTraceWithName:name]; NSTimeInterval time = fabs([comeDate timeIntervalSinceNow]); [trace setValue:[NSString stringWithFormat:@"%.2f秒",time] forAttribute:@"page_speed"]; DLog(@"PageName = %@,时间差(秒) = %.2f",name,time); [trace stop]; } 复制代码