2014-10-08 24 views
3

我正在尋找可以發送推送通知給ios設備和android設備的php代碼。蘋果推送通知爲iOS和谷歌雲消息傳遞爲Android使用PHP?

單獨我已經使用easyapns的ios推送通知和gcm的android推送通知。

我想合併兩個代碼,併爲這兩個設備發送推送通知的單個代碼。

我只在php部分工作。有什麼解決方案可以讓它變得簡單嗎?

+1

我已經解決了這個問題,在DB中有一個字段,告訴每個設備的類型。我分組他們,並推動Android設備在一起和IOS設備在一起 – zozelfelfo 2014-10-08 11:04:40

+0

我會感激,如果你提供我的代碼,因爲這是我所需要的。我已經在具有列設備類型的數據庫中創建了一個表。和入門將只有ios或Android – kabir 2014-10-08 11:14:44

+0

對不起,我沒有代碼,因爲我在Java做的,而不是PhP,但你可以使用相同的邏輯 – zozelfelfo 2014-10-08 11:20:00

回答

3

我的研究後,我發現了一個解決方案適用於iOS的工作的罰款。

public function send_notification($registration_id,$message,$ios_certificate,$badge,$tHost){ 
    // Provide the Host Information. 
    //$tHost = 'gateway.sandbox.push.apple.com'; 

    $tPort = 2195; 

    // Provide the Certificate and Key Data. 
    //$tCert = 'certificates/meetmethere/apns-dev-cert.pem'; 
    $tPassphrase = ''; 

    //$tToken = 'efdf10632aa4e711ef8c57abf2a3cdec51e7c42811bb998c3e26de2876bac8fa'; 
    //$tAlert = 'this is the test message '; 

    // The Badge Number for the Application Icon (integer >=0). 

    $tBadge = 0; 

    // Audible Notification Option. 
    $tSound = 'default'; 

    // The content that is returned by the LiveCode "pushNotificationReceived" message. 
    $tPayload = 'message'; 

    // Create the message content that is to be sent to the device. 
    $tBody['aps'] = array (
         'alert' => $message, 
         'badge' => $badge, 
         'sound' => $tSound, 
        ); 

    $tBody ['payload'] = $tPayload; 
    $tBody = json_encode ($tBody); 

    // Create the Socket Stream. 
    $tContext = stream_context_create(); 

    stream_context_set_option ($tContext, 'ssl', 'local_cert', $ios_certificate); 

    // Remove this line if you would like to enter the Private Key Passphrase manually. 

    stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase); 

    // Open the Connection to the APNS Server. 

    $tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext); 

    // Check if we were able to open a socket. 

    if (!$tSocket) 
     $status=false; 

    // Build the Binary Notification. 
    $tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $registration_id) . pack ('n', strlen ($tBody)) . $tBody; 

    // Send the Notification to the Server. 

    $tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg)); 

    if ($tResult) 
     $status=true; 
    else 
     $status=false; 
    fclose ($tSocket); 

      return $status; 
}` 
0

給你一些鏈接的希望,這樣可以幫助你在Android設備上發送推送通知。

,如果您有任何疑問,請讓我知道我可以幫你Send Push Notification on android devide

+0

我已經嘗試過這一點,它的工作,但只適用於android ..如果我使用ios設備? – kabir 2014-10-08 11:21:20

+0

[pushnotification to IOS](http://libcapn.org/php-apn/)檢查這個,我希望它能幫助你。 – 2014-10-08 12:17:38

+0

只適用於ios。但我結合了IOS和Android – kabir 2014-10-08 13:24:09