I am working on push notifications. I wrote the following code for fetching a device token.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"This is device token%@", deviceToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
NSLog(@"Error %@",err);
}
I am able to run application on device successfully but not able to get the device id on console.
I have no issues with certification and provisioning profiles.
To get Token Device you can do by some steps:
1) Enable APNS (Apple Push Notification Service) for both Developer Certification and Distribute Certification, then redownload those two file.
2) Redownload both Developer Provisioning and Distribute Provisioning file.
3) In Xcode interface: setting provisioning for PROJECT and TARGETS with two file provisioning have download.
4) Finally, you need to add the code below in AppDelegate file to get Token Device (note: run app in real device).
If you are still not getting device token, try putting following code so to register your device for push notification.
It will also work on ios8 or more.
Following code is use for the retrive the device token.
Get device token in Swift 3
In your Appdelegate, in the
didRegisterForRemoteNotificationsWithDeviceToken
method:You have to use following code to get device token:-
Objective-C
Swift 3.0