小專欄地址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 SDK網絡
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];
}
複製代碼