2

I am trying to implement push notification in iOS in PHP using APNS library. Here is my code :

<?php

// Put your device token here (without spaces):    
$deviceToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';    

// Put your private key's passphrase here:
$passphrase = 'XXXXXX';

// Put your alert message here:
$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));print_r($result);

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
?>

It show message sending successfully but in iOS it does not show notification.

2015-07-28 08:32:39 Connected to APNS result = 97
2015-07-28 08:32:39 Message successfully delivered 
2015-07-28 08:32:39 Connection closed to APNS

Why this is happening? Am I missing anything ?

2 Answers 2

1

his mistake is with the ios 9 if so stop using the code in objective C and use in swift as it has a bug in generating objectiv token - C for ios9. I recommend urgent change your code. Also through this thought was the server when the scheduled ios changed for swift everything was resolved and my php was almost equal. Care:

            $ msg = chr (0). pack ('N', 32). pack (H * ','. '$deviceToken [0].'). pack ('N', strlen ($ payload)). $ payload;     
0

There are two potential reason that is causing this:

  1. Your app is in foreground
  2. Your device token is invalid

In my app. I am directly getting deviceToken that's in NSData

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
    // this is producing something like:
    // <xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>
    // 
    // and i am directly saving that to my servers database

Here's what i have in my server.

$from_database = "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>";

// this removes the '<' and '>' to make the device token valid
//
$token_string = substr($from_database, 1, -1);

$token_array[] = $token_string;

// you can also add multiple 'token_string' to the 'token_array' for push notification broadcasting, i am currently doing that and it is working properly.

...

public function __push_notification($message_input, $token_array) 
{
    $message = stripslashes($message_input);

    $passphrase = 'xxxxxx';

    $cert = realpath('xxx.pem');

    $payload = '{
        "aps" :
            {
                "alert" : "'.$message.'",
                "badge" : 1, 
                "sound" : "bingbong.aiff"
            }
    }';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp) 
    {
        // echo "Failed to connect $err $errstr <br>";

        return;
    }
    else 
        // echo "Post notification sent<br>";

    $dev_array = array();

    $dev_array = $token_array;

    foreach ($dev_array as $device_token) 
    {
        $msg =  chr(0) .
                pack("n", 32) . 
                pack('H*', str_replace(' ', '', $device_token)) . 
                pack("n", strlen($payload)) . 
                $payload;

        // echo "sending message :" . $payload . "n";

        fwrite($fp, $msg);
    }
    fclose($fp);
}

Also check this, might be helpful: Facing problems in ios push notification php code

6
  • Yes.. First you need to log the ` $string_token` make sure the device token dont have '<' and '>' both ends.. another is uncomment the echos if (!$fp) and lastly comment fwrite($fp, $msg); to prevent from sending notification.. CHECK it using your browser of terminal..
    – 0yeoj
    Jul 28, 2015 at 7:22
  • echo "Failed to connect $err $errstr <br>"; is a good way for checking what's going on..
    – 0yeoj
    Jul 28, 2015 at 7:24
  • it goes into else part and print - Post notification sent Jul 28, 2015 at 7:26
  • Hmm.. make sure push notification is enabled for you app. check this tutorial for setting up Push notification (app side) I think the problem is not in your server..
    – 0yeoj
    Jul 28, 2015 at 7:30
  • With sandbox you can receive notifications? I think that this is his problem. He is sending to the sandbox server and that server acts as a virtual device (read Development here).
    – eloibm
    Jul 28, 2015 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.