• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Firebase 通知 Swift 3

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:15:17 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的主要错误是:

"Argument of #selector refers to instance method 'tokenRefreshNotification' that is not exposed to Objective-C"

这是我的 AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let gcmMessageIDKey = "gcm.message_id"

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        GMSServices.provideAPIKey("appKey")

        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)
    }
    // [START refresh_token]
    func tokenRefreshNotification(_ notification: Notification)
    {
        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID Token \(refreshedToken)")
        }

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

    // [START connect_to_fcm]
    func connectToFCM()
    {
        // Won'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("Unable to connect with FCM \(error)")
            } else {
                print("Connected to FCM")
            }
        })

    }
    // [END connect_to_fcm]

错误行是这样的:

NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil)

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

"Method cannot be marker@objc because the type of the parameter cannot be represented in Objective C"



Best Answer-推荐答案


选择器错误,应该是

NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification, name: .firInstanceIDTokenRefresh, object: nil)

选择器的末尾必须有一个“:”。

另外方法签名似乎是错误的,应该是

@objc func tokenRefreshNotification(notification: Notification) {

下划线太多了。

关于ios - Firebase 通知 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798405/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap