Please verify code as looks like this
first import local notification
import UserNotifications
then create a method
func settingPushNotification() {
let app = UIApplication.shared
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)
app.registerUserNotificationSettings(settings)
}
app.registerForRemoteNotifications()
}
you can call this method either in appdelegate
or in viewcontroller
this way.
self.settingPushNotification()
you need to add delegate methods
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
if !token.isEmpty {
let userDefaults = UserDefaults.standard
userDefaults.set(token, forKey: Strings.DeviceToken.rawValue)
}
print("Device Token: \(token)")
}
func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
Make sure you added push notification in signing and capabilities.
This way you can get APNS Device token.
some
Network operator. I changed my current Network operator and its start working again. And app successfully received APNS Push token. If anyone knows any solution for this problem? How to make sure Apple received APNS token request without any blocking? – Karalynn