Firebase使用集合

栏目: IOS · 发布时间: 5年前

内容简介:Firebase是一家实时后端数据库创业公司,它能帮助开发者很快的写出Web端和移动端的应用。自2014年10月Google收购Firebase以来,用户可以在更方便地使用Firebase的同时,结合Google的云服务地址因为我们公司是用户群是在台湾,所以在第三方库的选型上选择了Firebase。因为它拥有较丰富的第三方库集。3.添加Firebase配置文件

Firebase是一家实时后端数据库创业公司,它能帮助开发者很快的写出Web端和移动端的应用。自2014年10月Google收购Firebase以来,用户可以在更方便地使用Firebase的同时,结合Google的云服务地址

前言

因为我们公司是用户群是在台湾,所以在第三方库的选型上选择了Firebase。因为它拥有较丰富的第三方库集。

基础接入

  1. 创建Firebase开发者账户注册地址
  2. 安装Firebase.framework 通过Cocopod 安装
pod 'Firebase/Core'
复制代码

3.添加Firebase配置文件 将 Firebase 添加到您的 iOS 项目

  • 在控制台中创建项目后,下载配置文件GoogleService-Info.plist,移动加入项目中
  • 在didFinishLaunchingWithOptions 初始化firebase
[FIRApp configure]; 
复制代码

目前支持Firebase服务有

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' 机器学习套件(基于设备的面部检测)

基于Firebase下的GA统计

1.配合GTM SDK 将firebase采集到的数据发送到Google Analytics中

添加GTM SDK 与 Google Analytics SDK

pod 'GoogleIDFASupport'  // Google Analytics 
pod 'GoogleTagManager'   // GTM SDK
复制代码
  • 登录GTM的平台地址

    Firebase使用集合
  • 创建触发器

    Firebase使用集合
  • 创建代码 GA-ID变量即是 我们的Google Analytics平台的ID

    Firebase使用集合

简述:我们先在 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}];
}           
复制代码

FCM推送

  1. 通过cocopod库设置Fcm sdk, fcm 中支持通知消息和数据消息

  2. 在"专案设置-设置-Cloud Messaging"中上传开发者凭证到fcm平台

    Firebase使用集合
  3. 配置代码如下

//通过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.
}
复制代码

Performace监控

Firebase使用集合
firebase 的performance能采集到数据有链接
  • 启动速度
  • 网络呼叫成功率
  • 網路回應 MIME 類型
  • 網路回應延遲時間
  • 不同畫面的顯示速度緩慢資料 (可以自定义采集内容)
//通过Cocopod导入
pod 'Firebase/Performance'
复制代码

performance 只要解决导入库,即可。默认会在[FIRApp configure]; 已初始化

关于自定义采集 page_speed

Firebase使用集合
// 自定义采集是通过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];
}
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

谋局者

谋局者

何常在 / 北京联合出版公司 / 2017-1 / 39.80

★商战版《官场笔记》!全面超越《问鼎》《交手》!商战小说*大神何常在迄今为止至为满意之作! ★以马云、马化腾、李彦宏、雷军、刘强东、张朝阳等大佬为原型,写透高手们的大智慧、大手腕、大谋略! ★善谋者胜,善算者赢!内含大量阳谋诡计、商业运作、商业谈判、事件营销等可以读以致用的知识!是商界人士必看读物! ★全景再现互联网三大帝国七大诸侯从无到有从有到强从强到吞并一切的成长和并购史! ......一起来看看 《谋局者》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换