0

I am writing php code to send push notifications to multiple iphone devices.. While i am executing php code i am getting warning message in the below line

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

like

Warning: pack() [function.pack]: Type H: illegal hex digit in /myapp.com/pushgrams/pushtest.php on line 39

Can any one tell me how to solve this issue?

lalith458
  • 695
  • 2
  • 12
  • 30
  • `$deviceToken = $payload = ?` So that we can reproduce the error ? – HamZa Jun 27 '13 at 08:49
  • payload is `$payload = array(); $payload['aps'] = array('alert' => $message, 'badge' => 0, 'sound' => 'default'); $payload = json_encode($payload);` – lalith458 Jun 27 '13 at 08:52
  • `$deviceToken` is more important ? – HamZa Jun 27 '13 at 08:53
  • yes, $devicetoken is important and the devicetoken which i am giving is `a9e3a660924c2bf96b6540fe78697bed9fe85332e44323cbe4dbd6b04120eaaf` – lalith458 Jun 27 '13 at 08:54
  • I [can't reproduce](http://codepad.org/As3PQFyZ) your error. Are you sure that $deviceToken doesn't have tabs or something like that ? Try to use `preg_replace('#[\t\r\n\s]#', '', $deviceToken)` instead of `str_replace`. Also try to use `var_dump` which should give you the length of the variable. – HamZa Jun 27 '13 at 09:04
  • k.. it is working, but error new errors like `Warning: stream_socket_client() [function.stream-socket-client]: SSL: crypto enabling timeout in /myapp.com/pushgrams/pushtest.php on line 118 Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /myapp.com/pushgrams/pushtest.php on line 118 Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /myapp.com/pushgrams/pushtest.php on line 118 Connection Failed - iPhone Push Notifications Server 0` – lalith458 Jun 27 '13 at 09:24
  • so `preg_replace()` did the job ? – HamZa Jun 27 '13 at 09:25
  • yes. but i am getting the above warnings, how to solve this – lalith458 Jun 27 '13 at 09:27
  • 1
    StackOverflow isn't a personal helpdesk for debugging purposes. If this solved your problem and you have another one that is basically not related to it. You should do some research. When you get stuck, ask a *new* question. – HamZa Jun 27 '13 at 09:30
  • k. I have done lot of research about the above warnings, but i dint get any solution, So, i am asking you.. – lalith458 Jun 27 '13 at 09:33
  • @lalith458 Try using `tls://` instead of `ssl://` - although the above comment is correct in that this is a completely separate problem and should be addressed with a new question. Keep in mind that the main aim of StackOverflow is to generate quality content that others can easily find to help them solve similar problems - so if you have a distinct issue you should create a distinct question, showing what you have tried to solve *that* specific problems, so others can see the logical path that one would follow to resolve such a problem. – DaveRandom Jun 27 '13 at 09:34
  • @lalith458 I can't believe you since it's technically impossible to do `lot of research about the above warnings` in 10 minutes. – HamZa Jun 27 '13 at 09:35
  • I am researching about to solve the above issues since 2 days, but i dint any answer, so, i am asking you – lalith458 Jun 27 '13 at 09:37
  • i have already posted a new question about above issues in the below post "http://stackoverflow.com/q/17205925/1774312", but i dint get any suitable answers – lalith458 Jun 27 '13 at 09:40

1 Answers1

0

This is how you pack for Apple Push Notification:

// APNs packet prepare
function pck($token, $notification, $msg_id = 0, $expiration = 604800) {
  $token = str_replace(" ", "", $token);
  $pck  = pack('CNNnH*', 1, $msg_id, $expiration > 0 ? time() + $expiration : 0, strlen($token) / 2, $token) . 
          pack('n', strlen($notification)) . $notification;

  return $pck;
}

This is how you prepare the notification:

$m = '{"aps":{"alert":"' . $msg. '","sound":"default"},"acme2":["bang","whiz"],"acme3":["bing","bong"]}';
$p  = pck($k, $m);
transilvlad
  • 13,974
  • 13
  • 45
  • 80