2014-10-09 108 views
4

我試圖在生產環境中發送推送通知,但它不起作用。下面是我正在嘗試的代碼,它會超時。沒有錯誤,沒有例外被拋出。 這有什麼問題?APNS PHP推送通知不起作用。獲取超時

注意:當我使用沙盒(ENVIRONMENT_SANDBOX)和開發證書文件發送推送通知時,它可以工作。但生產證書文件和ENVIRONMENT_PRODUCTION不起作用。

<?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'); 
+0

這看起來像一個很好的問題,但我懷疑有很多PHP開發人員需要經驗來回答它。希望我能幫上忙。 – JSON 2014-10-15 16:43:17

+0

這有幫助嗎? http://stackoverflow.com/questions/19031862/apnsphp-push-notifications-working-in-development-but-not-in-production – andy 2014-10-16 19:50:57

+0

審閱它。 Thx Andy。 – Subharanjan 2014-10-17 13:09:31

回答

1

要使用生產環境,您的應用程序必須使用您的分發證書進行簽名。測試這種方法的唯一方法是提交到App Store並下載App Store版本的應用程序;或使用AdHoc構建。當您的應用程序使用您的開發證書進行簽名時,您無法使用生產推送環境。

如果您已經完成了所有這些工作,請確保您沒有對從開發環境獲得的生產環境使用相同的設備令牌。他們會有所不同。

+0

是的,我完成了兩個。使用生產證書並生成帶有生產細節的設備令牌。最初,我正在使用設備令牌的開發與生產.pem文件,這是拋出錯誤。現在,我已經制作了帶有製作細節的設備標記和.pem文件,但它沒有拋出任何錯誤/異常,而是在一段時間後超時。 – Subharanjan 2014-10-17 13:07:38

+0

你使用的是一個AdHoc構建?這是使用生產證書進行測試的唯一方法。否則它將無法工作。 – i40west 2014-10-17 17:03:38

+0

我已經嘗試開發,adhoc和商店。 :(仍然沒有工作,爲什麼不拋出任何錯誤??(headbang) – Subharanjan 2014-10-20 07:53:45