舊版Firebase v.3.17.0在iOS用Objective-C佈建FCM功能

因手上某個案子需要修改舊版Firebase的code, 讓他可以接收FCM發送過來的訊息,因此進行修改調整。有關設置Firebase等相關說明,可以參考這裡:iOS客戶端的註冊方法,以及Google plist設定與安裝,其他在https://firebase.google.com/docs/cloud-messaging/ios/client 的說明就不要仿照了,否則會出現不可思議的現象,因為這網頁的說明都是以Firebase 5.x以後版本的做法,對早期會出現其他問題,例如Google SignIn,以及Firebase其他應用,甚至會造成閃退。

有關舊的Firebase SDK安裝與設定的方式如下:

設定Firebase SDK,採用cocoapod設置

pod 'Firebase/Core'
pod 'Firebase/Messaging'

起始Firebase

Firebase初始化程式碼要添加到應用程式。導入Firebase模塊並配置共享實例,如下所示:

@import Firebase;
@import UserNotifications;
@import FirebaseInstanceID;
@import FirebaseMessaging;

註冊取得遠端通知,一般來說寫在AppDelegate.m裡面的application didFinishLaunchingWithOptions 裡面進行呼叫。

    [FIRMessaging messaging].remoteMessageDelegate= self;
    
    if ([UNUserNotificationCenter class] != nil) {
      // 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];
    
    [FIRApp configure];
    
    NSString *token = [[FIRInstanceID instanceID] token];
    _FCMTOKEN = token;  //passing FCMtoken to DirekSQLite
    NSLog(@"FCMToken: %@", token);

基本上就完成了,最重要是取得FCM Token,其他偵測說明可以參考google firebase的線上說明

發佈留言