【发布时间】:2016-01-02 06:30:56
【问题描述】:
我是 Android 应用程序开发的新手。现在我正在开发一个简单的应用程序,用户可以通过 PHP 脚本将数据上传到 MySQL 服务器。我能够成功地做到这一点。但我希望每当用户从他的应用程序上传一些数据到服务器时,所有其他应用程序用户都应该收到通知(即 xyz 用户添加了新帖子。)。
我找到了使用 Google Cloud Messaging 实现此目的的方法。但我无法在我的应用程序中实现它。
我已使用以下 PHP 脚本通过 Web 服务器上的 PHP 脚本成功实现了来自 Web 消息的 GCM 通知。
<?php
//Generic php function to send GCM push notification
function sendMessageThroughGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "AIzaSyCVQRSnEoulXoG21uKua1DTrOUvMkxbK-U");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
<?php
//Post message to GCM when submitted
$pushStatus = "GCM Status Message will appear here";
if(!empty($_GET["push"])) {
$gcmRegID = file_get_contents("GCMRegId.txt");
$pushMessage = $_POST["message"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$pushStatus = sendMessageThroughGCM($gcmRegIds, $message);
}
}
//Get Reg ID sent from Android App and store it in text file
if(!empty($_GET["shareRegId"])) {
$gcmRegID = $_POST["regId"];
file_put_contents("GCMRegId.txt",$gcmRegID);
echo "Done!";
exit;
}
?>
<html>
<head>
<title>Google Cloud Messaging (GCM) in PHP</title>
<style>
div#formdiv, p#status{
text-align: center;
background-color: #FFFFCC;
border: 2px solid #FFCC00;
padding: 10px;
}
textarea{
border: 2px solid #FFCC00;
margin-bottom: 10px;
text-align: center;
padding: 10px;
font-size: 25px;
font-weight: bold;
}
input{
background-color: #FFCC00;
border: 5px solid #fff;
padding: 10px;
cursor: pointer;
color: #fff;
font-weight: bold;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function(){
$("textarea").val("");
});
function checkTextAreaLen(){
var msgLength = $.trim($("textarea").val()).length;
if(msgLength == 0){
alert("Please enter message before hitting submit button");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<div id="formdiv">
<h1>Google Cloud Messaging (GCM) in PHP</h1>
<form method="post" action="/swach/gcm/gcm.php/?push=true" onsubmit="return checkTextAreaLen()">
<textarea rows="5" name="message" cols="45" placeholder="Message to send via GCM"></textarea> <br/>
<input type="submit" value="Send Push Notification through GCM" />
</form>
</div>
<p id="status">
<?php echo $pushStatus; ?>
</p>
</body>
</html>
并在我的设备上成功收到通知。
但我想设置该应用程序应自动通知 GCM 发送消息,而不是使用 WEB 脚本。 并且所有其他应用用户都应该收到通知。
我也经历过 GCM DownStream 和 UpStream 消息传递,但不太了解,无法成功实现。
我尝试使用以下代码设置下游,但我没有收到我的应用程序的通知
public class AddNewMessage extends Activity{
private Integer msgId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_message);
msgId = 1;
}
// OnClick Action to the button inside the Activity Layout
public void sendNewMessage(View view) {
final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
if (view == findViewById(R.id.send_message)) {
new AsyncTask<Void,Void,String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
Bundle data = new Bundle();
data.putString("my_message", "Hello World");
data.putString("my_action","SAY_HELLO");
String id = Integer.toString(msgId);
gcm.send(ApplicationConstants.GOOGLE_PROJ_ID + "@gcm.googleapis.com", id, data);
msg = "Sent message";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(AddNewMessage.this, "send message failed: " + msg, Toast.LENGTH_LONG).show();
}
}.execute(null, null, null);
}
}
}
感谢您的宝贵时间。
【问题讨论】:
-
你得到什么回应?
-
看看tounder.com。他们处理大多数服务器设置。他们有一个简单的Android SDK,并且他们支持很棒的用户体验操作,例如全局关闭和跟踪您的通知是打开还是关闭。这是一个例子:youtube.com/watch?v=77hfYzZml9s
标签: php android push-notification google-cloud-messaging