Firebase项目创建
必须先创建一个Firebase的项目,并将其关联到你的iOS应用,然后才能将Firebase集成到你的iOS应用,Firebase中文文档
一、创建Firebase项目
-
在Firebase控制台 中,点击
添加项目
,然后选择输入项目名称
-
如果你创建了一个新项目,可以修改
项目ID
firebase会自动为你的firebase项目分配唯一的ID,项目预配好资源后,将无法更改项目ID,因此,若要使用特定标识符,就必须在此设置好项目ID
-
点击继续
-
为你的项目设置Google Analytics(分析)(可选,自行选择)
-
点击创建项目,Firebase 会自动为您的 Firebase 项目预配资源。完成此过程后,您将进入 Firebase 控制台中 Firebase 项目的概览页面。
二、在Firebase控制台注册你的应用
-
在 Firebase 控制台的项目概览页面的中心位置,点击 iOS 图标进入设置
-
在
iOS软件包ID
字段输入你的应用的软件包ID
确保输入应用的实际ID,在注册后麻将无法修改此值,
软件包ID
就是项目的bundle ID
-
(可选)根据下一步提示,输入其他信息
- 应用别名: 就是在外面看到你的应用的名字(app名字)
- App store ID:Firebase 动态链接使用该 ID 将用户重定向到 App Store 页面,而 Google Analytics(分析)则用它将转化事件导入 Google Ads。 如果您的应用还没有 App Store ID,您可以稍后在项目设置中添加此 ID。
-
点击注册应用。
三、添加Firebase配置文件
- 点击下载
GoogleService-Info.plist
以获取firebase iOS配置文件 - 将文件移至Xcode项目的根目录
四、上传你的APNs身份验证秘钥
将您的 APNs 身份验证密钥上传到 Firebase。 如果您还没有 APNs 身份验证密钥,请参阅配置FCM APNs。
-
在 Firebase 控制台中,在您的项目内依次选择
齿轮图标
、项目设置
以及云消息传递
标签。 -
在 iOS 应用配置下的 APNs 身份验证密钥中,点击
上传
按钮。 -
转到您保存密钥的位置,选择该密钥,然后点击打开。添加该密钥的 ID(可在
Apple Developer Member Center 的 Certificates, Identifiers & Profiles
中找到),然后点击上传。
将Firebase集成到你的应用
-
使用pod安装,在podfile文件中添加
pod ‘Firebase/Messaging’
-
在AppDelegate.m文件中导入头文件并初始化Firebase
#import <Firebase.h> ... [FIRApp configure]; [FIRMessaging messaging].delegate = self;
-
在
didFinishLaunchingWithOptions:
注册接受远程通知if (@available(iOS 10.0, *)) { // 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]; } [application registerForRemoteNotifications];
-
遵守
FIRMessagingDelegate、UNUserNotificationCenterDelegate
协议,并实现方法#pragma mark - FIRMessagingDelegate - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken { NSLog(@"FCM registration token: %@", fcmToken); NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"]; [[NSNotificationCenter defaultCenter] postNotificationName: @"FCMToken" object:nil userInfo:dataDict]; } //app在后台 //iOS 6及以下 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"%@", userInfo); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"%@", userInfo); completionHandler(UIBackgroundFetchResultNewData); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error { NSLog(@"Unable to register for remote notifications: %@", error); } #pragma mark - UNUserNotificationCenterDelegate // app处在前台收到推送消息执行的方法 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)) { NSDictionary *userInfo = notification.request.content.userInfo; NSLog(@"%@", userInfo); completionHandler(UNNotificationPresentationOptionNone); } // ios 10以后系统,app处在后台,点击通知栏 app执行的方法 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(ios(10.0)){ NSDictionary *userInfo = response.notification.request.content.userInfo; NSLog(@"%@", userInfo); completionHandler(); }
推送测试
- 在设备上运行并安装该应用,需要接受权限请求,允许通知
- 打开通知编辑器,并选择
新建消息
- 输入内容后,点击
发送测试消息
- 输入设置的令牌(之前获取的token),点击测试,即可收到通知消息