IOSfirebase推送SDK接入说明

1.接入须知

1.需要配合龙图baseSDK使用。
3.接入龙图iOSSDK后,需要配置firebase需要的依赖库。

1.0 开发要求


  • iOS 11.0 及以上版本

  • XCode 14.1 及以上版本

  • 1.1 申请你的AppID

    1.需要配置的参数同firebase统计和谷歌登录参数一致;
    2.需要添加的GoogleService-Info.plist文件同firebase统计使用的是同一个文件。

    1.2 下载SDK包和示例Demo

    下载对应demo

    1.3 开发环境配置

    1、需要引入的三方库

    2、必须在 Xcode Build Settings 里面 Other Linker Flags 设置值 -ObjC。
    该项如果设置错误,运行时就会出现异常:unrecognized selector sent to instance exception

    3、设置SDK所需权限, 在项目工程的 signing&capabilities 中增加权限:

    2.SDK接入

    首先需要引入SDK:

    1. #import <LTFIRMessagingSDK/LTFIRMessagingSDK.h>

    2.1 初始化

    接口:

    1. /// 生命周期
    2. /// - Parameters:
    3. /// - application: application
    4. /// - launchOptions: launchOptions
    5. - (BOOL)LTFIRMessagingApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

    示例:

    1. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingApplication:application didFinishLaunchingWithOptions:launchOptions];

    2.2 处理通知

    接口:

    1. /// 处理通知
    2. /// - Parameter message: 应用程序接收的下行消息
    3. - (void)LTFIRMessagingAppDidReceiveMessage:(NSDictionary *)message;

    示例:

    1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    2. [[LTGameOCSDK shareInstance] LTSDKApplication:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    3. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingAppDidReceiveMessage:userInfo];
    4. completionHandler(UIBackgroundFetchResultNewData);
    5. }
    6. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    7. {
    8. [[LTGameOCSDK shareInstance] LTSDKApplication:application didReceiveRemoteNotification:userInfo];
    9. // LTGameSDK::GetInstance().DidReceiveRemoteNotification(application, userInfo);
    10. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingAppDidReceiveMessage:userInfo];
    11. }

    2.3 获取deviceToken

    接口:

    1. /// 获取deviceToken
    2. /// - Parameter deviceToken: 推送deviceToken
    3. - (void)LTFIRMessagingSetDeviceToken:(NSData *)deviceToken;

    示例:

    1. // 通知相关声明周期
    2. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    3. {
    4. const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    5. NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
    6. ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
    7. ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
    8. ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    9. NSLog(@"deviceToken1:%@", strToken);
    10. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingSetDeviceToken:deviceToken];
    11. [[LTGameOCSDK shareInstance] LTSDKApplication:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    12. }

    2.4 获取本地FCMToken

    接口:

    1. /// 获取本地FCMToken
    2. - (NSString *)LTFIRMessagingGetFCMToken;

    示例:

    1. NSString *strToken = [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingGetFCMToken];

    2.5 设置主题

    接口:

    1. /// 设置主题
    2. /// - Parameter topic: 主题
    3. - (void)LTFIRMessagingSubscribeToTopic:(NSString *)topic;

    示例:

    1. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingSubscribeToTopic:主题];

    2.6 获取当前的FCMToken

    接口:

    1. /// 获取当前的FCMToken
    2. - (void)LTFIRMessagingGetFCMTokenMessage;

    示例:

    1. [[LTFIRMessagingSDK ShareInstance] LTFIRMessagingGetFCMTokenMessage];
    2. 此接口前提需注册获取fcmtoken通知,调用此接口后会自动发送通知
    3. [[NSNotificationCenter defaultCenter] addObserver:self
    4. selector:@selector(FIRMessagingFCMTokenNotification:)
    5. name:@"LTFIRMessagingFCMTokenNoti"
    6. object:nil];
    7. - (void)FIRMessagingFCMTokenNotification:(NSNotification *)noti{
    8. NSDictionary *dict = noti.object;
    9. NSString *token = [dict objectForKey:@"message"];
    10. NSLog(@"token1 = %@",token);
    11. }

    2.7 接收fcmtoken刷新的通知

    示例:

    1. 此接口前提需注册fcmtoken刷新的通知
    2. [[NSNotificationCenter defaultCenter] addObserver:self
    3. selector:@selector(FIRMessagingReceiveNotification:)
    4. name:@"LTFIRMessagingdidReceiveNoti"
    5. object:nil];
    6. 该通知会在token每次刷新后自动发送
    7. - (void)FIRMessagingReceiveNotification:(NSNotification *)noti{
    8. NSDictionary *dict = noti.object;
    9. NSString *token = [dict objectForKey:@"fcmToken"];
    10. NSLog(@"token2 = %@",token);
    11. }