由于项目要推广在国外,以及考虑原生推送的种种问题.选择了 Firebase 的推送.在使用过程中遇到了一些坑,在此做一下总结.
首先吐槽一下 Firebase 的中文版,居然混杂着另外一种语言(有些地方感觉翻译居然和英文相差甚远),突然让感觉到全英的是多么美好.
进入正题.当然使用别人的 SDK,当然要引入人家的 SDK,就需要账号,还需要创建你的应用,填写相关的信息.需要注意的两点:1.记得上传推送 p12包括开发和发布的.2.下载 GoogleServer-info.plist 文件.
SDK导入好了, p12上传了, plist 也导入了.就需要到工程的 xxxAppDelegate.m文件进行代码的编写.
1.引入头文件
2.在 application:didFinishLaunchingWithOptions:中需要实现的代码
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[FIRApp configure];
// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)name:kFIRInstanceIDTokenRefreshNotification object:nil];
监听的方法:
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"tokenRefreshNotification InstanceID token: %@", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
}
}];
}
如果你选择了主题在下面方法中实现:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
dispatch_async(dispatch_get_main_queue(), ^{
//The FIRMessaging class handles topic messaging functionality. To subscribe to a topic, call subscribeToTopic:topic from your application's main thread (FCM is not thread-safe):
[[FIRMessaging messaging] subscribeToTopic:@"/topics/ yournews"];
NSLog(@"Subscribed to news topic");
});
}
需要注意的地方,就是 APNS的 devicetoken的注册给 Firebase,注意区分沙盒和发布.实现下面的代码:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
#if DEBUG
[[FIRInstanceID instanceID] setAPNSToken:deviceToken
type:FIRInstanceIDAPNSTokenTypeSandbox];
#else
[[FIRInstanceID instanceID] setAPNSToken:deviceToken
type:FIRInstanceIDAPNSTokenTypeProd];
#endif
}
如果你想对接收的推送信息进行处理可以实现在下面的方法中处理:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
}
到此就基本接近推送的尾声了,需要对 FCM 进入前台以及进入后台的处理
进入后台需要实现方法:
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[[FIRMessaging messaging] disconnect]; //Firebase
NSLog(@"Disconnected from FCM");
}
进入前台实现方法:
- (void)applicationDidBecomeActive:(UIApplication *)application {
//Firebase
[self connectToFcm];
}
其实文档说明也是非常详细的,还可以在 github 上下载其 demo .
网友评论