Для этого есть 2-3 причины.
- Firebase не настроен / не инициализирован в didFinishLaunchingWithOptions
- Токен FCM истек или недействителен и наблюдатель обновления маркера не добавлен
- Использование недействительного / другого GoogleService-Info.plist, т. Е. Если вы используете 2 GoogleService-Info.plist в одном проекте для Dev и production.поэтому убедитесь, что во время установки приложения правильный GoogleService-Info.plist получает
модуль 'Firebase / Messaging'
, если вы тоже используете InAppMessagingDisplay, затем установите этот модультакже
pod 'Firebase / InAppMessagingDisplay'
запуск Firebase на didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: CGRect(x: 0, y: 0, width: kDeviceWidth, height: kDeviceHeight))
FirebaseApp.configure()
NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
UNUserNotificationCenter.current().delegate = self
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
//You save or send fcm token to your sever
}
//+++++++++++++++++++++++
// FCM Token Get Refreshed
@objc func tokenRefreshNotification(_ notification: Notification) {
getFCMToken()
}
private func getFCMToken() {
InstanceID.instanceID().instanceID { (result, error) in
if error == nil {
if let result = result {
let fcmToken = result.token
//Save or send to your server
}
}
}
}