4

I am trying to send Push Notification on the Production environment, but its not working. Below is the code I am trying and it gets timed out. No error, no exceptions get thrown out. What is wrong in this ?

Note: When I send push notification using Sandbox(ENVIRONMENT_SANDBOX) and the Development certificate files, it works. However the Production certificate files and the ENVIRONMENT_PRODUCTION doesn't work.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

/**
 * @param string $device_token   unique device token
 * @param string $custom_message message that needs to be pushed
 * @param string $push_service   which service to use ( ApnsPHP/UrbanAirship )
 *
 * @return bool
 * @throws ApnsPHP_Exception
 * @throws ApnsPHP_Message_Exception
 * @throws ApnsPHP_Push_Exception
 * @throws Exception
 */
function send_apple_push_notification_test( $device_token, $custom_message, $push_service = 'ApnsPHP' ) {

    if ( empty( $device_token ) || empty( $custom_message ) ) {
        return false;
    }

    // Report all PHP errors
    error_reporting( -1 );

    // Adjust to your time-zone
    date_default_timezone_set( 'America/New_York' );

    // Using Autoload all classes are loaded on-demand
    require_once 'includes/ApnsPHP/Autoload.php';

    try {

        // Instantiate a new ApnsPHP_Push object
        $push = new ApnsPHP_Push(
            ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION, '/home/xxxxx/public_html/wp-content/themes/yyyyyy/includes/ApnsPHP-pem/AppPushNotify.pem'
        );

        // Set the Root Certificate Authority to verify the Apple remote peer
        $push->setRootCertificationAuthority( '/home/xxxxx/public_html/wp-content/themes/yyyyyy/includes/ApnsPHP-pem/Entrust_Root_Certification_Authority.pem' );

        // Connect to the Apple Push Notification Service
        $push->connect();

        // Instantiate a new Message with a single recipient
        $message = new ApnsPHP_Message_Custom( (string) $device_token );

        // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
        // over a ApnsPHP_Message object retrieved with the getErrors() message.
        $message->setCustomIdentifier( "xxxyyyzzz-" . time() );

        // Set a simple welcome text
        $message->setText( (string) $custom_message );

        // Play the default sound
        $message->setSound();

        // Set the expiry value to 30 seconds
        $message->setExpiry( 60 );

        // Set the "View" button title.
        $message->setActionLocKey( 'See the message.' );

        // Add the message to the message queue
        $push->add( $message );

        // Send all messages in the message queue
        $push->send();

        // Disconnect from the Apple Push Notification Service
        $push->disconnect();

        // Examine the error message container
        $aErrorQueue = $push->getErrors();
        print_r( $aErrorQueue );

        return true;

    } catch ( Exception $e ) {
        print_r( $e->getMessage() );
    }

    return false;
}

echo "start";
send_apple_push_notification_test( '20fcc5090eb1f539ac0fddd345r4d0c50e5bca071b742d3d9833f16dd97adeb', 'Test Msg for Production' );
Subharanjan
  • 668
  • 8
  • 21
  • This looks like a very good question, but I doubt there are many PHP developers with the required experience to answer it. Wish I could help. – JSON Oct 15 '14 at 16:43
  • Does this help? http://stackoverflow.com/questions/19031862/apnsphp-push-notifications-working-in-development-but-not-in-production – andy Oct 16 '14 at 19:50
  • Going through it. Thx Andy. – Subharanjan Oct 17 '14 at 13:09

1 Answers1

1

To use the production environment, your app must be signed with your distribution certificate. The only way to test this is either to submit to the App Store and download the App Store version of your app; or use an AdHoc build. You cannot use the production push environment when your app is signed with your development certificate.

If you’ve done all that, make sure you aren’t using the same device token for the production environment that you got from the development environment. They will be different.

Jeremy
  • 4,339
  • 2
  • 18
  • 12
  • Yes, I have done both. Using production certificate and generating device token with production details. Initially I was using device token of Development with Production .pem files which was throwing error. Now, that I have made the device token and .pem files with Production details, it is not throwing any error/exception, rather gets timed out after certain time. – Subharanjan Oct 17 '14 at 13:07
  • Are you using an AdHoc build? That’s the only way to test with Production certificates. It won’t work otherwise. – Jeremy Oct 17 '14 at 17:03
  • I have tried with dev, adhoc and also with store. :( Still not working. Why isn't it throwing any error ?? (headbang) – Subharanjan Oct 20 '14 at 07:53
  • increase ssl handshake time out. I had this problem in my java code. – Arun George Jan 23 '15 at 18:21