I found some posts already on stack overflow but they are old and not actual anymore.
I have a problem with implementing Push Notifications.
Xcode 11, iOS 13, App just created so the certificates are for Xcode 11 or later, Push Notifications & Backgroundmodes (processing, remote notifications, fetch) are enabled.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Firebase Messaging
registerForPushNotifications()
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
Messaging.messaging().delegate = self as MessagingDelegate
// //Locationtracking
//
// guard let isLocationEnabled = UserDefaults.standard.value(forKey: "userEnabledLocationTracking") as? Bool else {locationManager.stopLocationTracking(); return true}
//
// isLocationEnabled ? locationManager.startLocationTracking() : locationManager.stopLocationTracking()
return true
}
After that I am calling this 2 Functions:
func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
print("Permission granted: \(granted)")
guard granted else {return}
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {return}
DispatchQueue.main.async {
print("register called.......")
UIApplication.shared.registerForRemoteNotifications()
}
}
}
But my "didRegisterForRemoteNotificationsWithDeviceToken" - Delegate method is not being called and I cannot understand why this happens.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Auth.auth().setAPNSToken(deviceToken, type: .unknown)
Messaging.messaging().apnsToken = deviceToken
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
}
Would be great if anyone can help me and tell me where's my mistake at.
Have a nice day!