iOS-集成Firebase发送推送通知到app

Firebase项目创建

必须先创建一个Firebase的项目,并将其关联到你的iOS应用,然后才能将Firebase集成到你的iOS应用,Firebase中文文档

一、创建Firebase项目
  1. 在Firebase控制台 中,点击 添加项目 ,然后选择输入项目名称

  2. 如果你创建了一个新项目,可以修改项目ID

    firebase会自动为你的firebase项目分配唯一的ID,项目预配好资源后,将无法更改项目ID,因此,若要使用特定标识符,就必须在此设置好项目ID

  3. 点击继续

  4. 为你的项目设置Google Analytics(分析)(可选,自行选择)

  5. 点击创建项目,Firebase 会自动为您的 Firebase 项目预配资源。完成此过程后,您将进入 Firebase 控制台中 Firebase 项目的概览页面。

二、在Firebase控制台注册你的应用
  1. 在 Firebase 控制台的项目概览页面的中心位置,点击 iOS 图标进入设置

  2. iOS软件包ID字段输入你的应用的软件包ID

    确保输入应用的实际ID,在注册后麻将无法修改此值,软件包ID就是项目的bundle ID

  3. (可选)根据下一步提示,输入其他信息

    • 应用别名: 就是在外面看到你的应用的名字(app名字)
    • App store ID:Firebase 动态链接使用该 ID 将用户重定向到 App Store 页面,而 Google Analytics(分析)则用它将转化事件导入 Google Ads。 如果您的应用还没有 App Store ID,您可以稍后在项目设置中添加此 ID。
  4. 点击注册应用。

三、添加Firebase配置文件
  1. 点击下载GoogleService-Info.plist以获取firebase iOS配置文件
  2. 将文件移至Xcode项目的根目录
四、上传你的APNs身份验证秘钥

将您的 APNs 身份验证密钥上传到 Firebase。 如果您还没有 APNs 身份验证密钥,请参阅配置FCM APNs

  1. 在 Firebase 控制台中,在您的项目内依次选择齿轮图标项目设置以及云消息传递标签。

  2. 在 iOS 应用配置下的 APNs 身份验证密钥中,点击上传按钮。

  3. 转到您保存密钥的位置,选择该密钥,然后点击打开。添加该密钥的 ID(可在Apple Developer Member Center 的 Certificates, Identifiers & Profiles 中找到),然后点击上传。

将Firebase集成到你的应用

  1. 使用pod安装,在podfile文件中添加

    pod ‘Firebase/Messaging’

  2. 在AppDelegate.m文件中导入头文件并初始化Firebase

    #import <Firebase.h>
    ...
    
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
    
  3. 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];
    
  4. 遵守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();
    }
    

推送测试

  1. 在设备上运行并安装该应用,需要接受权限请求,允许通知
  2. 打开通知编辑器,并选择新建消息
  3. 输入内容后,点击发送测试消息
    在这里插入图片描述
  4. 输入设置的令牌(之前获取的token),点击测试,即可收到通知消息
    在这里插入图片描述
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 Flutter 中,配置 Firebase 推送证书文件是通过在 iOS 应用中添加配置文件(.plist 文件)来实现的。下面是配置过程的详细步骤: 1. 在 Firebase 控制台中启用推送通知功能,并下载 APNs 证书文件(.p12)。 2. 打开 Xcode 项目,在项目导航器中选择 iOS 应用的 Runner,然后选择 File > New > File。 3. 在弹出的对话框中选择 iOS > Resource > Property List,并点击 Next。 4. 在下一个对话框中,输入文件名,选择所在的目录,然后点击 Create。 5. 在 Property List 编辑器中,添加以下键值对: ``` <key>FirebaseMessaging</key> <dict> <key>APNSToken</key> <data>[APNs 证书文件的 base64 编码字符串]</data> </dict> ``` 其中,`[APNs 证书文件的 base64 编码字符串]` 是 APNs 证书文件的 base64 编码字符串,可以通过以下命令获取: ``` openssl pkcs12 -in [APNs 证书文件名].p12 -out [输出文件名].pem -nodes -clcerts base64 -i [输出文件名].pem ``` 运行以上命令后,将输出的 base64 编码字符串复制到 `data` 标签中即可。 6. 保存 Property List 文件,并将其添加到 Xcode 项目中。 7. 在 Xcode 项目中,选择 Runner > Targets > Signing & Capabilities,然后点击 + Capability,选择 Push Notifications。 8. 如果需要在应用程序处于后台时显示推送通知,还需要在 Capabilities 中启用 Background Modes,并选择 Remote notifications。 完成上述步骤后,就可以在 Flutter 中使用 Firebase 推送通知功能了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值