Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get device token for push notification

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.

like image 489
jagzzz Avatar asked Jan 10 '12 04:01

jagzzz


People also ask

How can we get device token for Android for push notification?

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.

What is device token in push notification?

Push tokens are generated by push service providers. Braze connects with push service providers like Firebase Cloud Messaging Service (FCMs) for Android and Apple Push Notification Service (APNs) for iOS, and those providers send unique device tokens that identify your app.


2 Answers

NOTE: The below solution no longer works on iOS 13+ devices - it will return garbage data.

Please use following code instead:

+ (NSString *)hexadecimalStringFromData:(NSData *)data {   NSUInteger dataLength = data.length;   if (dataLength == 0) {     return nil;   }    const unsigned char *dataBuffer = (const unsigned char *)data.bytes;   NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];   for (int i = 0; i < dataLength; ++i) {     [hexString appendFormat:@"%02x", dataBuffer[i]];   }   return [hexString copy]; } 

Solution that worked prior to iOS 13:

Objective-C

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  {     NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];     token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];     NSLog(@"this will return '32 bytes' in iOS 13+ rather than the token", token); }  

Swift 3.0

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {     let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})     print("this will return '32 bytes' in iOS 13+ rather than the token \(tokenString)") } 
like image 88
Wasif Saood Avatar answered Sep 23 '22 00:09

Wasif Saood


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).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {          [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(@"%@", str); }  - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {       NSString *str = [NSString stringWithFormat: @"Error: %@", err];      NSLog(@"%@",str); } 
like image 37
Bkillnest Avatar answered Sep 20 '22 00:09

Bkillnest