У меня есть поддержка приложений для iOS 10 и iOS 9, использующая Firebase для удаленного уведомления администратора.
Мой делегат приложения:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
//TODO Confirm if this is the right point to initialize Push Notifications
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
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})
} else {
// iOS 9 or before
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification), name: NSNotification.Name.MessagingRegistrationTokenRefreshed, object: nil)
// [END register_for_notifications]
return true
}
@ доступно (iOS 10, *) расширение AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID iOS10: \(messageID)")
}
// Change this to your preferred presentation option
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
}
расширение AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("NOTIFICATION: Registration token => \(fcmToken)")
if !UserDefaultsManager.isRegisterToken {
verifyStore(fcmToken: fcmToken)
}
}
// [END refresh_token]
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("NOTIFICATION: Received data message => \(remoteMessage.appData)")
}
// [END ios_10_data_message]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
verifyStore(fcmToken: fcmToken)
}
}
, но для iOS9 я не понимаюкогда он получил токен FCM.Мне нужно запустить метод при регистрации и обновлении токена в iOS9.Вы можете мне помочь?