iOS集成Firebase云消息传递(FCM)

简介

Firebase 云消息传递 (FCM) 是一种跨平台消息传递解决方案,可供您免费、可靠地传递消息。
使用 FCM,您可以通知客户端应用有新的电子邮件或其他数据有待同步。您可以发送通知消息进行用户再互动并留住他们。在即时通讯等使用情形中,一条消息可将最多 4KB 的有效负载传送至客户端应用。

工作原理

FCM 实现包括用于发送和接收的两个主要组件:

  1. 一个受信任的环境,例如 Cloud Functions for Firebase 或用于构建、定位和发送消息的应用服务器。
  2. 一个接收消息的 iOS、Android 或网页 (JavaScript) 客户端应用。

您可以通过 Firebase Admin SDK 或 FCM 服务器协议发送消息。为了利用强大的内置定位和分析功能来测试或发送营销或互动消息,您还可以使用通知编辑器。

image.png

集成云消息传递:

准备工作:
*   安装 Xcode 10.1 或更高版本。
*   安装 CocoaPods 1.4.0 或更高版本。
*   在 Xcode 中打开您的项目。
*   您的项目必须适用于 iOS 8 或更高版本。
*   Swift 项目必须使用 Swift 3.0 或更高版本。
*   设置用于运行您的应用的一台 iOS 真机设备或 iOS 模拟器。
*   为了支持云消息传递,您需要:
    *   一台 iOS 真机设备
    *   一个 [Apple 开发者帐号](https://www.google.com/url?sa=D&q=https%3A%2F%2Fdeveloper.apple.com%2Faccount)所对应的 Apple 推送通知身份验证密钥(即APNs Auth Key(APNs key ID和Team ID),也可以使用APNs推送证书的.p12文件)
*   在 Xcode 中通过 App > Capabilities 启用推送通知功能。
*   对于所有其他 Firebase 产品,您可以使用 iOS 真机设备或 iOS 模拟器。
*   使用您的 Google 帐号[登录 Firebase](https://console.firebase.google.com/)。
第 1 步:创建 Firebase 项目

您必须先创建一个 Firebase 项目,并将其关联到您的 iOS 应用,然后才能将 Firebase 添加到您的 iOS 应用。访问了解 Firebase 项目以详细了解相关信息。
创建 Firebase 项目

image.png

image.png
第 2 步:在 Firebase 中注册您的应用

有了 Firebase 项目后,在项目中添加自己的iOS应用

  1. 在 Firebase 控制台的项目概览页面的中心位置,点击 iOS 图标以启动设置工作流。
    如果您已向 Firebase 项目添加了应用,请点击添加应用以显示平台选项。
  2. 在 iOS 软件包 ID 字段中输入应用的软件包 ID。
    确保输入应用实际使用的 ID。
  • 在 XCode 中打开您的应用,然后在顶级 Runner 目录中访问常规标签,找到此软件包 ID。软件包标识符字段的值是 iOS 软件包 ID(例如 com.yourcompany.yourproject)。
    注意:bundle ID是唯一的。在向 Firebase 项目注册应用后,将无法添加或修改此值。
  1. (可选)根据设置工作流的提示输入其他应用信息。
  • 应用昵称:方便内部使用的标识符,只有您能在 Firebase 控制台中看到
  • App Store ID:Firebase 动态链接使用该 ID 将用户重定向到 App Store 页面,而 Google Analytics for Firebase 则用它将转化事件导入 Google Ads。如果您的应用还没有 App Store ID,您可以稍后在项目设置中添加此 ID。
  1. 点击注册应用。
第 3 步:添加 Firebase 配置文件
  1. 点击下载 GoogleService-Info.plist 以获取您的 Firebase iOS 配置文件 (GoogleService-Info.plist)。
    • 此文件支持重复下载 Firebase iOS 配置文件。
    • 此文件名称必须是“GoogleService-Info.plist”不可更改。
  2. 将配置文件移至 Xcode 项目的根目录中。如果出现提示,请选择将配置文件添加到所有目标。
    如果您的项目中有多个软件包 ID,则必须将每个软件包 ID 与 Firebase 控制台中的注册应用相关联,使每个应用都有自己的 GoogleService-Info.plist 文件。
注意:该 Firebase 配置文件包含项目的唯一、非机密标识符。
第 4 步:将 Firebase SDK 添加到您的应用
  1. 如果您没有 Podfile,请创建一个: cd your-project-directory pod init
  2. 将想要用在应用中的 pod 添加到 Podfile。例如,对于 Analytics: pod 'Firebase/Analytics' 此 pod 会在您的 iOS 应用中添加 Firebase 正常运行所需的必备库以及 Google Analytics for Firebase 功能。
  3. 安装 pod,然后打开 .xcworkspace文件以便在 Xcode 中查看该项目:
pod install
open your-project.xcworkspace
第 5 步:在应用中初始化 Firebase

添加初始化代码。

  1. 在 UIApplicationDelegate中导入 Firebase 模块并签订代理:
@import Firebase;

签订代理

如:


image.png
  1. 配置一个 FirebaseApp 共享实例(通常在应用的 application:didFinishLaunchingWithOptions:方法中配置):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
    
    //注册远程通知。这显示了第一次运行时的权限对话框to
    //在更合适的时间显示对话框,相应移动此注册。
    if (@available(iOS 10.0, *)) {
        if ([UNUserNotificationCenter class] != nil) {
            // iOS 10 or later
            // For iOS 10 display notification (sent via APNS)
            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
            UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
            UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
            [[UNUserNotificationCenter currentNotificationCenter]
             requestAuthorizationWithOptions:authOptions
             completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 // ...
             }];
        } else {
            // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
            UIUserNotificationType allNotificationTypes =
            (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings =
            [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
            [application registerUserNotificationSettings:settings];
        }
    } else {
        // Fallback on earlier versions
    }
    
    [application registerForRemoteNotifications];
    NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    
    [self initRootVC];
    return YES;
}

代理方法实现

// [START receive_message]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //如果你在后台接收到一条通知消息,
    //    这个回调将不会被触发,直到用户点击通知启动应用程序。
    // TODO:处理通知数据
    
    //当swizzling被禁用时,你必须让消息传递知道消息,以便进行分析
    // [FIRMessaging消息]appdid应收款emessage:userInfo];
    
    //打印消息ID。
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }
    
    // Print full message.
    NSLog(@"%@", userInfo);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    //如果你在后台接收到一条通知消息,
    //    这个回调将不会被触发,直到用户点击通知启动应用程序。
    // TODO:处理通知数据
    
    //当swizzling被禁用时,你必须让消息传递知道消息,以便进行分析
    // [FIRMessaging消息]appdid应收款emessage:userInfo];
    
    //打印消息ID。
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }
    
    // Print full message.
    NSLog(@"%@", userInfo);
    
    completionHandler(UIBackgroundFetchResultNewData);
}
// [END receive_message]

// [START ios_10_message_handling]
//接收iOS 10设备的显示通知。
//当app在前台时处理传入的通知消息。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    
    //当swizzling被禁用时,你必须让消息传递知道消息,以便进行分析
    // [FIRMessaging消息]appdid应收款emessage:userInfo];
    
    //打印消息ID。
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }
    
    // Print full message.
    NSLog(@"%@", userInfo);
    
    //将此更改为您首选的显示选项
    if (@available(iOS 10.0, *)) {
        completionHandler(UNNotificationPresentationOptionNone);
    } else {
        // Fallback on earlier versions
    }
}

//后台运行: 指的是程序已经打开, 用户看不见程序的界面, 如锁屏和按Home键.
//程序退出: 指的是程序没有运行, 或者通过双击Home键,关闭了程序.**
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }
    
    // Print full message.
    NSLog(@"%@", userInfo);
    
    completionHandler();
}

// [END ios_10_message_handling]

// [START refresh_token]
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"FCMToken" object:nil userInfo:dataDict];
    // TODO:如果需要,发送令牌到应用服务器。
    //注意:这个回调在每次应用程序启动时以及每次生成新令牌时触发。
}
// [END refresh_token]

// [START ios_10_data_message]
//当应用程序在前台时,直接从FCM(绕过APNs)接收iOS 10+上的数据消息。
//要启用直接数据消息,可以设置[消息传递消息传递]。shouldEstablishDirectChannel是的。
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
    NSLog(@"Received data message: %@", remoteMessage.appData);
}
// [END ios_10_data_message]

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Unable to register for remote notifications: %@", error);
}

//在这里添加这个函数只是为了调试,如果启用了swizzling,则可以删除它。
//如果swizzling被禁用,那么必须实现这个函数,以便APNs设备令牌可以被配对
// FCM注册令牌。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"APNs device token retrieved: %@", deviceToken);
    //将来需要将此Token上传给后台服务器
    //禁用swizzling后,必须在这里设置APNs设备令牌。
    // (FIRMessaging消息)。APNSToken = deviceToken;
}

添加初始化代码后,运行您的应用以便向 Firebase 控制台发送验证信息,证明您已成功安装 Firebase。

你可能感兴趣的:(iOS集成Firebase云消息传递(FCM))