My problem is not being solved after I've tried a lot of questions which might have the solution for this issue:
I want to get FCM Token
. In AppDelegate.swift
I've imported
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
After that I've extended it with UNUserNotificationCenterDelegate, MessagingDelegate
and then in didFinishLaunchingWithOptions
I've added:
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
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
And after that I've added these four functions:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Here")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
Now the issue is when I run the app it shows the alert which ask for permissions for sending notification. Allowing it or disallowing it is not my concern I have implemented MessagingDelegate
method didRefreshRegistrationToken
so it should be called, but it is not calling that method. So am I missing something?
You have to add an Observer like that
And add this method in your Appdelegate like that
Hope it helps.
As mentioned in the Firebase Docs you can either:
FIRMessagingDelegate
protocolkFIRMessagingRegistrationTokenRefreshNotification
If you take the first option, you need to set the delegate
You can see this done in the quickstart example code