I somehow can not receive the device token when registering for remote notifications. I get the modal saying "Do you want to allow App X to be able to send you notificaitons", but when I accept it, the didRegisterForRemoteNotifications function is not called.
When I register for remote notifications in iOS 8/Swift using this code:
UIApplication.sharedApplication().registerForRemoteNotifications()
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
These functions are not triggered at all:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!)
and
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!)
however when I log this
println("current settings \(UIApplication.sharedApplication().currentUserNotificationSettings()) and \(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())")
I receive
"current settings <UIUserNotificationSettings: 0x170437120; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);> and true"
My provisioning profile and certificates ar all in order.
Has someone else had this problem?
Install either Xcode or iPhone Configuration Utility Highlight your device name on the left, and go to the Console tab. Copy the above, remove spaces if present. This is your device token.
To receive the Device Token (and updates to the token value) and push notifications, you must create a custom class that extends FirebaseMessagingService . The onNewToken callback fires whenever a new token is generated.
Whenever your Application is installed first time and open, MyFirebaseMessagingService created and onNewToken(String token) method called and token generated which is your Device Token or FCM Token.
For swift 4.0 please follow some steps :
1).On push notification in capabilities(TARGET)
2).Use this code in didFinishLaunchingWithOptions
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
3).Add this also
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)")
}
You can try this
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
var deviceTokenString: String = (deviceToken.description as NSString)
.stringByTrimmingCharactersInSet(characterSet)
.stringByReplacingOccurrencesOfString( " ", withString: "") as String
println(deviceTokenString)
}
EDIT: Update for Swift 2.x
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
EDIT: Update for Swift 3.x
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let characterSet = CharacterSet(charactersIn: "<>")
let deviceTokenString = deviceToken.description.trimmingCharacters(in: characterSet).replacingOccurrences(of: " ", with: "");
print(deviceTokenString)
}
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