我已经在现有应用中配置了GCM,并且在那里收到了通知。现在我面临两个问题:1)我退出应用程序时未收到通知,或者应用程序在后台。2)我没有在iPhone的通知区域中收到通知,仅当我的应用程序正在运行时,我才直接在该区域接收警报消息。当我下拉通知区域时,我在xcode的控制台中收到此消息“无法连接到GCM:操作无法完成。(com.google.gcm错误2001。)”
我的PHP文件在下面
<?php
// Payload data you want to send to iOSdevice(s)
// (it will be accessible via intent extras)
$data = array( 'message' => 'Hello World!');
// The recipient registration tokens for this notification
// http://developer.android.com/google/gcm/
$ids = array( 'kucy6xoUmx********eeRsla' );
// 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 = 'AIz******9JA';
// Define URL to GCM endpoint
$url = 'https://gcm-http.googleapis.com/gcm/send';
// Set GCM post variables (device IDs and push payload)
$post = array(
'registration_ids' => $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 );
}
?>