iOS中使用新的Firebase消息传送SDK进行通知 [英] Notification in iOS using new Firebase Messaging SDK
问题描述
非常感谢。
首先阅读Firebase文档。 => Firebase文档
-
在Firebase上注册项目=> 在这里注册项目
从这里获取GoogleService-Info.plist文件=> project =>设置=>常规
-
GoogleService-Info.plist文件放在您的项目中。 >在Firebase中设置通知.p12证书(制作和开发)
=>项目=>设置=>云消息传递
-
-
在你的pro中创建SDK文件夹现在将这个框架添加到你的Xcode中=> libicucore.tbd $ /
$ b
- 在Xcode中设置背景模式=> Projects => Capabilities => Background Mode ON => RemoteNotification
$ -
在Firebase上注册项目=> 在这里注册项目
从这里获取GoogleService-Info.plist文件=> project =>设置=>常规 -
GoogleService-Info.plist文件放在您的项目中。 >在Firebase中设置通知.p12证书(制作和开发)
=>项目=>设置=>云消息传递 -
在你的pro中创建SDK文件夹现在将这个框架添加到你的Xcode中=> libicucore.tbd $ /
$ b - 在Xcode中设置背景模式=> Projects => Capabilities => Background Mode ON => RemoteNotification
b
$ b
#importAppDelegate.h#importFirebase.h#importAFNHelper.h @interface AppDelegate(){NSString * InstanceID;} @ property(nonatomic,strong)NSString * strUUID; @property(nonatomic,strong)NSString * strDeviceToken; @ end @ implementation AppDelegate-(BOOL)application:(UIAppli阳离子*)应用didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {UIUserNotificationType allNotificationTypes =(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; [FIRApp配置]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification :) name:kFIRInstanceIDTokenRefreshNotification object:nil];返回YES;} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler :( 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:@%@,[通知对象]]; [self connectToFcm]; } - (void)connectToFcm {[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error){if(error!= nil){NSLog(@Unable to connect to FCM。%@,error); } else {//你可以用api或其他方式发送你的令牌......}}
Recently google announce the new Firebase SDK with lot more feature. So, I'm looking for the perfect documentation of How to implement Remote Notification feature using new Firebase Messaging SDK(FCM) in iOS.
Thanks a lot in advance.
Integrate without CocoaPods
First Read Firebase Doc. => Firebase Doc
Register project on Firebase here => Register Project here
Get GoogleService-Info.plist file from here=> project=> settings => General
GoogleService-Info.plist file drop in your project.
set Notification .p12 Certificates (Production and Development) in Firebase => project=> settings => Cloud Messaging
Download Firebase SDK here => Firebase SDK Download
Create SDK folder in your project and Drop all SDK Folder in it.
Now Add this Framework in your Xcode => libicucore.tbd
- Set Background Modes ON in Xcode => Projects => Capabilities => Background Mode ON => RemoteNotification
In Objective-c your Appdelegate.m file
#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....
}
}
这篇关于iOS中使用新的Firebase消息传送SDK进行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!