14

I am trying to create platform endpoint in amazon sns from the console. Below is the device token which I received from FCM/APNs:

fUG5dIcN_pA:APA91bEciRwWuvTIezAKcJ5y1xz5z6BygE3YJkywdCGCFJD93NTfjARwPRommwgsfvVo2iH_qZWT7D2Lxnc69uanato1UUq-nLl5R1L0qF4exT7zjM9Wdy9Evs6h-EOBtIVv7Vv8bPE1

I am getting an error: iOS device tokens must be no more than 400 hexadecimal characters.looks like APNs has sent token in string format and Amazon is expecting it in hexadecimal chars.

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
narendra
  • 199
  • 3
  • 8
  • 2
    I'm not an expert on this, but I think you are getting the wrong device token. See https://stackoverflow.com/questions/30821090/ios-device-token-example-for-push-notifications – viz Dec 15 '17 at 04:49
  • Did you find a solution for this? – Christian Jun 18 '18 at 10:03

3 Answers3

8

Try the below code in didRegisterForRemoteNotificationsWithDeviceToken:

let deviceToken = deviceToken.map {String(format:"%02.2hhx",$0)}.joined()
print(deviceToken)

And Paste the device token in amazon SNS console, it will work.

wscourge
  • 10,657
  • 14
  • 59
  • 80
Surya
  • 602
  • 5
  • 14
  • How to write this in objective c ? I got error: `Property 'map' not found on object of type 'NSData *'` – simo Jun 03 '19 at 11:59
1

For objective-c you can use this:

NSString * deviceTokenString = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                        stringByReplacingOccurrencesOfString: @">" withString: @""] 
                       stringByReplacingOccurrencesOfString: @" " withString: @""];

NSLog(@"The generated device token string is : %@",deviceTokenString);
DionizB
  • 1,487
  • 2
  • 10
  • 20
  • thanks, but I have converted the result back to `NSData` format, as the function `-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken` returns `[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:data];` and I got the same error: `Invalid parameter: Token Reason: iOS device tokens must be no more than 400 hexadecimal characters` – simo Jun 10 '19 at 09:11
  • Did you try the solution I gave or not? – DionizB Jun 10 '19 at 09:13
  • Yes, I did, but how to pass the result without have to convert it to `NSData ` ? if I try to pass it as is, I got error: `-[__NSCFString bytes]: unrecognized selector sent to instance` – simo Jun 10 '19 at 09:15
  • You tried this `NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];`? to convert to NSData? – DionizB Jun 10 '19 at 09:19
  • Yes, I tried `NSData *data = [deviceTokenString dataUsingEncoding:NSUTF8StringEncoding];` – simo Jun 10 '19 at 09:22
  • Did you check this thread? https://forums.aws.amazon.com/thread.jspa?messageID=572827 Since you need it as NSData not NSString, and it wouldn't make sense to convert twice the token – DionizB Jun 10 '19 at 09:31
  • I saw similar discussions, what they do is create the SNS endpoint inside `didRegisterForRemoteNotificationsWithDeviceToken` but I am trying to create it server side to avoid using SNS apis within the app it self – simo Jun 10 '19 at 09:36
  • Thanks DionizB, I am trying to avoid store SNS credentials within the app for the sake of security – simo Jun 10 '19 at 09:42
  • What exactly your code do? maybe I can re-wrote in using Ruby – simo Jun 10 '19 at 13:42
  • It removes occurrences of `<`, `>` and ` ` (empty space), from the token, maybe if you try to remove them on your side it might work – DionizB Jun 10 '19 at 14:07
  • How to do same for react native? – Dhrupal Aug 15 '20 at 10:45
0

I experienced this error after iOS 13. Since iOS 13 the token provided via the app delegate changed and if the delegate code is not updated for iOS 13 then an invalid token will be sent up.

Check this stack overflow link here for an example of how to update.

David Rees
  • 6,792
  • 3
  • 31
  • 39