Reputation: 172
iPhone device is not receiving device token from my application. The
didRegisterForRemoteNotificationsWithDeviceToken
method is not getting called though I have the following code in the
didFinishLaunch
method.
In didFinishLaunch
Method:
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
Note:
notificationSettings
method is getting called. but not didRegister
or didFail
methods called for Remote Notifications.What need to be checked to get the device token?
Upvotes: 0
Views: 263
Reputation: 2777
try this in didFinishLaunch
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
// ios 10
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in
// actions based on whether notifications were authorized or not
}
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
else
{
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
Upvotes: 1
Reputation: 47059
You are using UNUserNotificationCenter
but it only work if your device with iOS 10 or 10+.
For iOS 9 you need below code for register push notification
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
You can add condition based on iOS version like @Kamalesh answer.
Upvotes: 1