This is my first time working on with firebase due to a requirement by the backend so let me know if you don't understand the question or if it doesn't make any sense.
What I want to achieve is that I need a Registration_ID
which is the FCM Token when user register. So this is how I'm implementing it in AppDelegate.swift
> didFinishLaunchingWithOptions
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 {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
After that in AppDelegate.swift
I've added these three functions:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
No the issue is that when it ask for permission of notification and user deny it then how I'll get FCM that I'll send to the backend when user register or login.
An FCM token is generated regardless of the user's response to the authorization request - if the response takes long enough, the FCM token will be generated before the response is given.
Notifications will not work until the user gives permission but generating the FCM token is not dependent on getting permission.
Please note: I have not found this documented anywhere, but through my own testing. I have tested on iOS 10.3.1 and 11.0.
The first time the token is generated the Messaging
delegate's didRefreshRegistrationToken
is called, I suspect the problem is that you have not set the delegate with
Messaging.messaging().delegate = self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With