由于項目要推廣在國外,以及考慮原生推送的種種問題.選擇了 Firebase 的推送.在使用過程中遇到了一些坑,在此做一下總結(jié).
首先吐槽一下 Firebase 的中文版,居然混雜著另外一種語言(有些地方感覺翻譯居然和英文相差甚遠),突然讓感覺到全英的是多么美好.
進入正題.當然使用別人的 SDK,當然要引入人家的 SDK,就需要賬號,還需要創(chuàng)建你的應(yīng)用,填寫相關(guān)的信息.需要注意的兩點:1.記得上傳推送 p12包括開發(fā)和發(fā)布的.2.下載 GoogleServer-info.plist 文件.
SDK導(dǎo)入好了, p12上傳了, plist 也導(dǎo)入了.就需要到工程的 xxxAppDelegate.m文件進行代碼的編寫.
1.引入頭文件
2.在 application:didFinishLaunchingWithOptions:中需要實現(xiàn)的代碼
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];
監(jiān)聽的方法:
- (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.");
}
}];
}
如果你選擇了主題在下面方法中實現(xiàn):
- (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,注意區(qū)分沙盒和發(fā)布.實現(xiàn)下面的代碼:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
#if DEBUG
[[FIRInstanceID instanceID] setAPNSToken:deviceToken
type:FIRInstanceIDAPNSTokenTypeSandbox];
#else
[[FIRInstanceID instanceID] setAPNSToken:deviceToken
type:FIRInstanceIDAPNSTokenTypeProd];
#endif
}
如果你想對接收的推送信息進行處理可以實現(xiàn)在下面的方法中處理:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
}
到此就基本接近推送的尾聲了,需要對 FCM 進入前臺以及進入后臺的處理
進入后臺需要實現(xiàn)方法:
- (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");
}
進入前臺實現(xiàn)方法:
- (void)applicationDidBecomeActive:(UIApplication *)application {
//Firebase
[self connectToFcm];
}
其實文檔說明也是非常詳細的,還可以在 github 上下載其 demo .