我在android中开发了一个简单的应用程序,只是为了从一个用户发送通知到另一个用户。作为我的应用程序的快速概述:
我的问题很多时候都没有通知收件人。
我说的Java代码,只需在DB中搜索reg ID,然后在与GCM服务器通信时将其提交给PHP服务器和PHP服务器。
PHP代码:
<?php
$con = mysqli_connect("localhost","username","my password","database name");
// Replace with real BROWSER API key from Google APIs
$apiKey = "my api key";
// Replace with real client registration IDs
$destinationcereg = mysqli_real_escape_string($con,$_REQUEST['destreg']);// this will provided by the app
$registrationIDs = array($destinationcereg);
// Message to be sent
$destinationcereg2 = &$destinationcereg;
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => 'You Recieved New 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_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
echo ahmed;
?>
另请注意,有时很长一段时间后收到通知,大部分时间都没有收到,有时也会立即收到。
任何人都能帮帮我吗?提前谢谢。