1

我有这个脚本用于在我的 android 应用程序上发送推送通知:

<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyAATjQOVDdOUiHra0H-EfxpobQcqOsdwqa";

// Replace with real client registration IDs. Put the correct registration id you are getting from app. If you are using eclipse check your logcat you will get it there.
$registrationIDs = array("APA91bEmxWS9bsXLsixaYQ6zuByM0SzFWe5DEKVLO68923hW3Mo2qB6bIH2LarP5WgzKasMtFAdVyy8YQuwv0YbrRNwdGFORh1wQvQ9uKBkC3jH6uXBYAQOv5xfzrsjYjqcQK8syikrQ6dq6oRrp9XUnimdj_4oBbw" );

// Message to be sent
$message = "Push working!";


// Set POST variables.
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array( "message" => $message ) /*Make sure that message is the key you are using in GCMIntentService.java onMessage() ->  extras.getString("message");*/
);

$headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );


// Execute post
$result = curl_exec($ch);


// Close connection
curl_close($ch);

// Echo success or failure
echo $result;

?>

我从来没有在我的手机上收到任何推送通知。问题是,实际上我从来没有从脚本中得到任何输出,就像echo $result;没有返回一样。

这甚至可能吗?

有任何想法吗?

解决方案


<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyAATjQOVDdOUiHra0H-EfxpobQcqOsdwqa";

// Replace with real client registration IDs. Put the correct registration id you are getting from app. If you are using eclipse check your logcat you will get it there.
$registrationIDs = array("APA91bEmxWS9bsXLsixaYQ6zuByM0SzFWe5DEKVLO68923hW3Mo2qB6bIH2LarP5WgzKasMtFAdVyy8YQuwv0YbrRNwdGFORh1wQvQ9uKBkC3jH6uXBYAQOv5xfzrsjYjqcQK8syikrQ6dq6oRrp9XUnimdj_4oBbw" );

// Message to be sent
$message = "Push working!";


// Set POST variables.
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array( "message" => $message ) /*Make sure that message is the key you are using in GCMIntentService.java onMessage() ->  extras.getString("message");*/
);

$headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );


// Execute post
$result = curl_exec($ch);


// Close connection
curl_close($ch);

// Echo success or failure
echo $result;

?>

现在它完美地工作了。

4

1 回答 1

0

发现错误。

换行换curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

我更正了问题上的代码,所以现在它可以工作了。

于 2013-03-07T10:21:42.087 回答