2016-04-04 1 views
0

自分のデバイスのGCM登録トークンを受信しましたが、PHPからREST APIを使用してメッセージを送信しているときに、成功を示していますが、デバイスにメッセージを受信して​​いません。 PHPコードREST APIを使用してPHPからデータを送信すると、Androidデバイスが通知を受信して​​いませんか?

<?php 
$data = array('message' => 'Hello World! First Message from PHP'); 
$ids = 'device registration token'; 
// Send a GCM push 
sendGoogleCloudMessage( $data, $ids); 

function sendGoogleCloudMessage($data, $ids) 
{ 
// Insert real GCM API key from Google APIs Console 
// https://code.google.com/apis/console/   
$apiKey = 'API Key'; //(server API Key) 
// Define URL to GCM endpoint 
$url = 'https://gcm-http.googleapis.com/gcm/send'; 
// Set GCM post variables (device IDs and push payload)  
$post = array(
       'to' => $ids, 
       'data'    => $data, 
       ); 

// Set CURL request headers (authentication and type)  
$headers = array( 
        'Authorization: key=' . $apiKey, 
        'Content-Type: application/json' 
       ); 

// Initialize curl handle  
$ch = curl_init(); 

// Set URL to GCM endpoint  
curl_setopt($ch, CURLOPT_URL, $url); 

// Set request method to POST  
curl_setopt($ch, CURLOPT_POST, true); 

// Set our custom headers  
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

// Get the response back as string instead of printing it  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// Set JSON post data 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); 

// Actually send the push 
$result = curl_exec($ch); 

// Error handling 
if (curl_errno($ch)) 
{ 
    echo 'GCM error: ' . curl_error($ch); 
} 

// Close curl handle 
curl_close($ch); 

// Debug GCM response  
echo $result; 
print_r($result); 
} 
?> 

PHPファイルを提出し、それは以下のようにメッセージを返しながら、そのメッセージは「成功」を示しているが、何のメッセージがデバイスで受信していないようです。追加の措置を講じる必要があるか、間違ったことをする必要がある場合は、

{"multicast_id":8550022549195779350,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1459795352923689%21f96bfff9fd7ecd"}]} 

答えて

0

APIキーが正しいこと、およびサーバーIPが証明書によって信頼されていることを確認してください。場合によっては、WiFi接続されたデバイスがしばらくしてGCMに応答しなくなることがあります。デバイスを再起動してください。送信したすべてのメッセージの受信を開始する必要があります。

また、このSO questionから、android:permission="com.google.android.c2dm.permission.SEND"の代わりにandroid:permission="com.google.android.gcm.c2dm.permission.SEND"を誤って使用したかどうかを確認してください。

希望すると便利です。

+0

感謝@abielita、アクセス許可は正しいです。メッセージの解析に関する問題だから問題は、アプリは、メッセージを受信して​​います。 – prasoons

関連する問題