代码之家  ›  专栏  ›  技术社区  ›  H.Epstein

FCM ios配置

  •  0
  • H.Epstein  · 技术社区  · 6 年前

    我正在尝试使用FCM进行推送通知。我遵循文档并尝试使用:

    InstanceID.instanceID().instanceID { (result, error) in
        if let error = error {
            print("Error fetching remote instange ID: \(error)")
        } else if let result = result {
            print("Remote instance ID token: \(result.token)")
            self.instanceIDTokenMessage.text  = "Remote InstanceID token: \(result.token)"
        }
    }
    

    didFinishLaunchingWithOptions ?

    我得到这个编译错误:

    静态成员“instanceID”不能用于类型为的实例 '实例ID'

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kuldeep Jayesh Lathiya    6 年前

    添加观察者 didFinishLaunchingWithOptions .

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
    }
    

    叫它进去 didRegisterForRemoteNotificationsWithDeviceToken

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        self.connectToFcm()
    }
    

    方法

    @objc func tokenRefreshNotification(_ notification: Notification) {
        self.connectToFcm()
    }
    

    func connectToFcm() {
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instange ID: \(error)")
            }
            else {
                print("FCM Token = \(String(describing: result?.token))")
                print("Remote instance ID token: \(result.token)")
                self.instanceIDTokenMessage.text  = "Remote InstanceID token: \(result.token)"
            }
        }
    }
    
        2
  •  1
  •   Ilia    4 年前

    我敢肯定,一些人通过这里,并经历了这个问题是没有解决他们的问题,由库尔德普给出的解决办法。

    我也有同样的问题。

    内部 didRegisterForRemoteNotificationsWithDeviceToken 而不是使用 InstanceID.instanceID().instanceID capacitor guide ,请执行此操作。

      func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: Messaging.messaging().fcmToken )
      }
    

    与标准的唯一区别 是否通过设备调用寄存器来删除通知 Messaging.messaging().fcmToken .

    在上找到解决方案 medium . 你会期望这个或者至少是一个工作版本会出现在。。。这取决于你遵循哪一个指南,我使用了所有的电容器指南,但需要这个来测试fcmToken通知。媒体指南中有更多关于fcmToken的内容,因此,根据您的用例,查看此指南可能是一个好主意。