2015-05-12 63 views
3

發送推送通知從服務器到Android應用程序我使用以下腳本。發送推送通知到Android應用程序開發鈦(跨平臺)使用PHP

<?php 
    $message = "hi there"; 
    $apiKey = "xxxxxxxxxxxx"; 
    $registrationIDs = array("xxxxxxx"); 
    $url = 'https://android.googleapis.com/gcm/send'; 
    // Set POST variables 
    $fields = array(
     'registration_ids' => $registrationIDs, 
     'data' => array("message" => $message, 
         ) 
         ); 
    $headers = array(
     'Authorization: key=' . $apiKey, 
     'Content-Type: application/json' 
    ); 
    $ch = curl_init(); // Open connection 
    curl_setopt($ch, CURLOPT_URL, $url); 
    // Set the url, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
    $result = curl_exec($ch); // Execute post 

    if($result === false) 
     die('Curl failed ' . curl_error()); 

    curl_close($ch); 
    //return $result; 
    // curl_close($ch);   // Close connection 
    $response = json_decode($result); 
    print_r($response); 
?> 

此代碼工作正常的原生Android應用程序,但在鈦開發當我發送推送通知,上面的腳本應用程序,設備接收通知,但與「NULL」​​。

我想知道,爲什麼?我的PHP腳本有什麼問題。

UPDATE

這是我的代碼接收通知

// These events monitor incoming push notifications 
CloudPush.addEventListener('callback', function (evt) { 
    //Ti.API.info('This is the payload data i got'+JSON.parse(evt.payload)); 
    Ti.API.log("This is the GCM response "+JSON.stringify(evt)); 
    alert(evt.payload); 
    //var payload = JSON.parse(evt.payload); 
    //Ti.API.info('This is the message data i got'+payload.message); 

}); 
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) { 
    Ti.API.info('Tray Click Launched App (app was not running)'); 
}); 
CloudPush.addEventListener('trayClickFocusedApp', function (evt) { 
    Ti.API.info('Tray Click Focused App (app was already running)'); 
}); 

,這是響應

[INFO] : APSCloudPush: receivePayload: null 
[INFO] : APSCloudPush: background: true 
[INFO] : APSCloudPush: queuePayload: null 
[INFO] : APSCloudPush: showTrayNotification 
[ERROR] : APSCloudPush: Payload is null! 
+0

沒有錯的腳本,顯示接收推送消息 –

+0

您的鈦代碼@MurtazaKhursheedHussain請採取更新的問題,一起來看看。 – AkshayP

+0

您正在使用哪種版本的CloudPush? –

回答

2

上面給出的代碼是完全正確的。我想這個問題只是關鍵字有效載荷。只需在服務器端腳本中將「message」一詞替換爲「payload」,如下所示。

$fields = array(
    'registration_ids' => $registrationIDs, 
    'data' => array("payload" => $message, 
        ) 
    ); 

由於鈦ti.cloudePush模塊內部搜索詞的有效載荷

+0

只有關鍵字替換才能真正解決問題? – AkshayP

+0

它適用於我,謝謝 – AkshayP

+0

感謝您關於'message' - >'payload'的提示,這也解決了我的問題。 –

相關問題