菜鸟教程小白 发表于 2022-12-11 19:15:17

ios - Firebase 通知 Swift 3


                                            <p><p>我的主要错误是:</p>

<blockquote>
<p>&#34;Argument of #selector refers to instance method &#39;tokenRefreshNotification&#39; that is not exposed to Objective-C&#34;</p>
</blockquote>

<p>这是我的 AppDelegate:</p>

<pre><code>@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let gcmMessageIDKey = &#34;gcm.message_id&#34;

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {

      GMSServices.provideAPIKey(&#34;appKey&#34;)

      FIRApp.configure()

      //Firebase
      if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(options: authOptions,
                                                                  completionHandler: {_, _ in})

            // For iOS 10 data message (sent via FCM)
            FIRMessaging.messaging().remoteMessageDelegate = self
      } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [ .alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
      }

      application.registerForRemoteNotifications()


      // Add observer for iNSTANCEid TOKEN REFRESH CALLBACK.
      NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil)

      // Override point for customization after application launch.
      return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    //
    func tokenRefreshNotification(_ notification: Notification)
    {
      if let refreshedToken = FIRInstanceID.instanceID().token() {
            print(&#34;InstanceID Token \(refreshedToken)&#34;)
      }

      // Connect to FCM since connection may have failed when attempted before having a token
      connectToFCM()
    }
    //

    //
    func connectToFCM()
    {
      // Won&#39;t connect since there is no token
      guard FIRInstanceID.instanceID().token() != nil else {
            return
      }

      // Disconnect previous FCM connection if it exists
      FIRMessaging.messaging().disconnect()

      FIRMessaging.messaging().connect(completion: {(error) in
            if error != nil {
                print(&#34;Unable to connect with FCM \(error)&#34;)
            } else {
                print(&#34;Connected to FCM&#34;)
            }
      })

    }
    //
</code></pre>

<p>错误行是这样的:</p>

<pre><code>NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil)
</code></pre>

<p>如果我将 @objc 添加到 tokenRefreshNotification 我会收到一个新错误:</p>

<blockquote>
<p>&#34;Method cannot be marker@objc because the type of the parameter cannot be represented in Objective C&#34;</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>选择器错误,应该是</p>

<pre><code>NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification:), name: .firInstanceIDTokenRefresh, object: nil)
</code></pre>

<p>选择器的末尾必须有一个“:”。</p>

<p>另外方法签名似乎是错误的,应该是</p>

<pre><code>@objc func tokenRefreshNotification(notification: Notification) {
</code></pre>

<p>下划线太多了。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Firebase 通知 Swift 3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42798405/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42798405/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Firebase 通知 Swift 3