Alright, so I'm implementing some push notifications in my app. I've written some PHP which should interact with a MySQL DB.
I want to store the device tokens in SQL as char(64) because I've come to believe this is good (I mean, it's supposed to be sent as binary later anyways).
On the iOS side I do this:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *tokenString = [deviceToken base64EncodedString];
NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys:tokenString, @"Token", [NSNumber numberWithBool:YES], @"IsiDevice", [NSNumber numberWithBool:YES], @"Active", nil];
//... http request stuff
// what might be important is that the POST body is encoded as UTF8 (content-type set appropriately)
}
The base64 stuff comes from NSData+Base64 by Matt Gallagher. Now, on the PHP side, this happens:
// $tokenEncoded is exactly the same string as I sent from the app (type is string)
$tokenEncoded = $jsonData['Token'];
// $token becomes null! (type is string)
$token = base64_decode($tokenEncoded);
From php.net/manual on base64_decode's return values:
Returns the original data or FALSE on failure. The returned data may be binary.
Now, I don't know more PHP then I've taught myself the last two days; but if the documentation says it should return FALSE or the original data, I find it very confusing that the returned value is a null-string (because I'm very sure the original data wasn't null!).
As the base64_decoded string becomes null, so does apparently my whole query string when I append it. That does not make for a very convincing query.
So my questions are, I guess: why does base64_decode return things it shouldn't? Once I actually get the data out of base64_decode, how should I include it in the query so that it is stored as I want it to in the DB (just append?)?
Additional info: I'm using the Slim Framework. I'm doing all this in a route. $jsonData is taken from the body like such:
$jsonBody = Slim::getInstance()->request()->getBody();
$jsonData = json_decode($jsonBody, true);
Did some additional tests from the app, just to make sure nothing gets fudged up by my app's implementation of base 64:
DLOG(@"Device token as NSData: %@", deviceToken);
NSString *base64token = [deviceToken base64EncodedString];
DLOG(@"Device token as base64: %@", base64token);
DLOG(@"Device token back from base64: %@", [NSData dataFromBase64String:base64token]);
Output:
Device token as NSData: <65625f1c c33b9480 5a46f346 8b2877d0 95e92823 33e88e91 fd3a2abf febad972>
Device token as base64: ZWJfHMM7lIBaRvNGiyh30JXpKCMz6I6R/Toqv/662XI=
Device token back from base64: <65625f1c c33b9480 5a46f346 8b2877d0 95e92823 33e88e91 fd3a2abf febad972>