简体   繁体   中英

Apple Push Notifications System Not Receving Message! (PHP)

This is the code I wrote to send apple push notifications. in the device_tokens i'm sending an array with all of the tokens I want to send message to. For some reason, I stopped recieving messages from this system.

and it used to work before!

private function send_iphone_notification($device_tokens, $message) 
{
    define("PRODUCTION_MODE", true);

    $apnsPort = 2195;

    // Choose dev or production apns host and certificate
    if(PRODUCTION_MODE) {
        $apnsHost = 'gateway.push.apple.com';
        $apnsCert = $_SERVER['DOCUMENT_ROOT'].'/ant/www/apns/PushIphone.pem';
    } else {
        $apnsHost = 'gateway.sandbox.push.apple.com';
        $apnsCert = $_SERVER['DOCUMENT_ROOT'].'/ant/www/apns/CertificatesDev.pem';
    }
    // Notification content
    $payload = json_encode(array('notification_from_panel' => 1, 'aps' => array('alert' => $message, 'badge' => 1, 'sound' => 'default')));

    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
    stream_context_set_option($streamContext, 'ssl', 'passphrase', "");

    // loop all devices tokens
    foreach ($device_tokens as $device_token) 
    {                       
        try {
            $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
        } catch(Exception $e) {
            echo $e->getMessage();
            continue;
        }
        $deviceToken = str_replace(" ","",substr($device_token,1,-1));
        //echo $deviceToken;
        $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(mb_strlen($payload)) . $payload;
        fwrite($apns, $apnsMessage);

        //socket_close($apns);
        fclose($apns);
    }

}

I have some experience with Apple's push service and i have to say that apple seems to have soem restrictions in place.

First of all you dont need to close and open the socket connection for every message. That will save you a lot of time. On the other hand you have to check if the connection is broken before you send the next message.

Anyway, it seemed that apple does not like it much when you connect a hundred times a second and send messages. Apples has blocked me for that as well in the passed.

But when you send the message with a already established connection, and at some point the connection is returning an error/ is disconnected, you dont know at what message he stopped. But you can define a identifier for each message and check the error returned when the connection is broken.

I noticed that apples server returns the error with a couple of seconds delay. That makes it hard do deal with it the easy way.

I have written a class to handle all of that but did not jet have time to publish it! If you are interested in it, check the github project for it at https://github.com/tinned-software/PHP-Tinned-MobilePush

I hope to finish the documentation soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM