I am writing a .c apns (apple push notification server) application,
I have the rest of the program working ie connecting to the servers establishing ssl, json encoding the message etc.
However I am stuck on the part that converts the token to a hex string part.
Example values
$deviceToken is "4DBCD414F624842E581972E65D2DAA4B96279B209BD0CE10AB12E52AEA48A670"
$apnsMessage is "{"aps":{"alert":"testing","badge":1,"sound":"elephant.aiff"}}"
Here is a php snippet which does the job at the moment.
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($message)) . $message;
fwrite($this->apnsConnection, $apnsMessage);
Any help would be appreciated. Thanks Peter
chr
function in PHP takes an integer and returns a string with one char which is the ASCII char equivalent of the input. Example chr(48)
would return "0"
. In C you can do it using snprintf
as:
int val = 30;
char str[2];
snprintf(str,2,"%c",val);
.
in PHP is the string concatenation, in C you can do it using strcat
and strcpy
function. Example:
$msg = "a" . "b";
in C would be:
char msg[MAX];
strcpy(msg,"a");
strcat(msg,"b");
strlen
works the same :) in both C and PHP. It takes a string and returns its length.
There is no direct function to do the job of str_replace
. You'll have to run though the string testing each char.
Example: To simulate str_replace('a','b',$str)
:
for(i=0;str[i];i++) {
if(str[i] == 'a') {
str[i] = b;
}
}