iOS中使用新的Firebase消息传送SDK进行通知

最近谷歌宣布了更多function的新的Firebase SDK。 因此,我正在寻找如何在iOS中使用新的Firebase消息传送SDK(FCM)实现远程通知function的完美文档。

非常感谢。

没有CocoaPods集成

首先阅读Firebase文档。 => Firebase文档

  1. 在此注册Firebase项目=>在此注册项目

  2. 从这里获取GoogleService-Info.plist文件=> project => settings => General

  3. GoogleService-Info.plist文件放在您的项目中。

  4. 在Firebase中设置通知.p12证书(生产和开发)=> project => settings => Cloud Messaging

  5. 下载Firebase SDK => Firebase SDK下载

  6. 在您的项目中创buildSDK文件夹,并将其中的所有SDK文件夹。

  7. 现在添加这个框架在你的Xcode => libicucore.tbd

  8. 在Xcode中设置背景模式=> Projects => Capabilities => Background Mode ON => RemoteNotification

在这里输入图像说明

在Objective-c中你的Appdelegate.m文件

 #import "AppDelegate.h" #import "Firebase.h" #import "AFNHelper.h" @interface AppDelegate (){ NSString *InstanceID; } @property (nonatomic, strong) NSString *strUUID; @property (nonatomic, strong) NSString *strDeviceToken; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; [FIRApp configure]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil]; return YES; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; NSLog(@"userInfo=>%@", userInfo); } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd]; NSLog(@"deviceToken1 = %@",deviceToken); } - (void)tokenRefreshNotification:(NSNotification *)notification { NSLog(@"instanceId_notification=>%@",[notification object]); InstanceID = [NSString stringWithFormat:@"%@",[notification object]]; [self connectToFcm]; } - (void)connectToFcm { [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Unable to connect to FCM. %@", error); } else { // you can send your token here with api or etc.... } }