找到了一种非常简单的方法。
打开http://phpfiddle.org/
在框中粘贴以下 php 脚本。在php脚本中设置API_ACCESS_KEY,设置设备id,以逗号分隔。
按 F9 或单击运行。
玩得开心;)
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array("YOUR DEVICE IDS WILL GO HERE" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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 );
curl_close( $ch );
echo $result;
?>
对于 FCM,google url 将是:https://fcm.googleapis.com/fcm/send
对于 FCM v1 google url 将是:https://fcm.googleapis.com/v1/projects/YOUR_GOOGLE_CONSOLE_PROJECT_ID/messages:send
注意:在谷歌开发者控制台上创建 API 访问密钥时,你必须使用 0.0.0.0/0 作为 ip 地址。 (用于测试目的)。
如果收到来自 GCM 服务器的无效注册响应,请交叉检查您的设备令牌的有效性。您可以使用以下网址检查您的设备令牌的有效性:
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=YOUR_DEVICE_TOKEN
一些响应代码:
以下是您可能从服务器收到的一些响应代码的描述。
{ "message_id": "XXXX" } - success
{ "message_id": "XXXX", "registration_id": "XXXX" } - success, device registration id has been changed mainly due to app re-install
{ "error": "Unavailable" } - Server not available, resend the message
{ "error": "InvalidRegistration" } - Invalid device registration Id
{ "error": "NotRegistered"} - Application was uninstalled from the device