Android的推送服务,实现GCM服务器端 [英] Android push service, implementing the gcm server side

查看:170
本文介绍了Android的推送服务,实现GCM服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的新到Android推动世界,我一直在挣扎了一下,为几天了。我创建并实现它的GCM客户端没有问题。我还创建了我的谷歌云计算项目,使机器人推notif.s并得到了我的项目数项目号 API密钥

I am all new to the android push world and I have been struggling a little, for couple of days now. I created and implemented the GCM client side of it without a problem. I also created my google cloud project, enabled android push notif.s and got my Project Number, Project ID and API Key.

到目前为止好,那么我希望得到的实施,但由于某种原因,在服务器端,我不知道我该怎么做,我应该在哪里写的code?难道要像REST风格的Web服务?我一直在挖 http://developer.android.com/google/gcm/ccs。 HTML 但我得到了我应该做的没有好结果。我还没有找到这个合适的教程在互联网上也是如此,他们都对推像 C2DM 去precated版本。我真的AP preciate的帮助。 问候。

So far so good, then I wanted to get to the implementing the server side of it but for some reason I have no idea what I should do, where should I write the code? Is it going to be a web service like restful? I have been digging the http://developer.android.com/google/gcm/ccs.html but I got no good outcome of what I should do. I haven't found a proper tutorial about this on the internet too, they are all about the deprecated versions of push like C2DM. I'd really appreciate the help. Regards.

注:我使用的是MSSQL

Note: I'm using Mssql.

推荐答案

我认为有在上,我根据我的服务器开发者文档的教程。现在,我看它,我无法找到它,所以我会后我的code进行砍伐的版本。

I thought that there was a tutorial in the developer docs on which I based my server. Now that I look for it, I can't find it, so I'll post a cut down version of my code.

我有一个树莓派作为服务器,PHP和MySQL数据库运行的Apache。予选择装置和一个项目组合并且经由网页发送消息到该应用程序,设备上。 我做了选择,在PHP中,根据项目ID和业主/设备给我弄一个注册ID该消息将被发送。

I have a Raspberry Pi as a server, running Apache with PHP and a MySQL database. I select a device and a project combination and send a message to that app on that device via a web page. I do a select in the PHP, based on the project ID and the owner/device to get me a registration ID to which the message will be sent.

要保持这个答案简单,我会告诉你如何将消息发送到设备,其中REGID和API密钥是很难codeD。你可以把你自己的数据库的东西在它附近以后。

To keep this answer simple, I'll show you how to send a message to a device where the RegId and the API Key are hard coded. You can put your own DB stuff around it later.

首先在 vals.php 文件,该文件包含我的/你的秘密硬codeD的东西

Firstly the vals.php file which holds my/your secret hard coded stuff

<?php
   $regidOne="Your regid for one phone/project combination";
   $apiKey = "Your API KEY"; 
?>

其次持有的形式以及对消息的按钮entry.php

Secondly the entry.php which holds the form and the button for the message

entry1.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<?php session_start();?>
<body>

<br>
Compose a message below

  <form action="send_it.php" method="post">
    <div id="a" align="left">   
     <P><TEXTAREA name="message" rows="3"   cols="58"></TEXTAREA><br>
            <INPUT type="submit" value="Click to send the message">
     </P>
    </div>
  </form>

<?php 
  $ret=$_SESSION['retval'];
  echo "Last message status: "; 
  echo $ret; 
  echo"<br><br>";
?>
</body>
</html>

最后文件( send_it.php ),它并不通过卷曲的工作

Lastly the file (send_it.php) which does the work via curl

<?php session_start();
   require 'vals.php';
   $randomNum=rand(10,100); 
   $registrationIDs[] = $regidOne;
   $message =  strip_tags($_POST['message']);


   $url = 'https://android.googleapis.com/gcm/send';
   $fields = array(
           'registration_ids' => $registrationIDs,
           'data' => array( "message" => $message), 
           'delay_while_idle'=> false,
           'time_to_live' => 86400, 
           'collapse_key'=>"".$randomNum.""
            );
   $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, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));

   // Execute post
   $result = curl_exec($ch);
   // Close connection
   curl_close($ch);
   echo "Your message has been sent, the results from the Cloud Server were :\n";
   echo "<p>";
   echo $result;
   $targets = array("{", "\"");
   $interim  =  str_replace($targets, " ", $result);
   $pieces = explode(",", $interim);
   $pos = strpos($result, 'multicast');
   if ($pos === false) {
     $ret = "Failed";
     $_SESSION['retval']=$ret;

   } else {
     $ret = "Sent OK, ";

     $_SESSION['retval']=$ret.$pieces[0];//Just the multicast id
   }
   echo "<br>"; echo "JSON parsed"; echo "<br>";
   $jres = json_decode($result);
   print_r($jres);

   $retloc =  'Location:'.$_SERVER['HTTP_REFERER'];
   if(!strstr($message, "skipheader")) {
      header($retloc);   
      // COMMENT OUT THE LINE ABOVE TO SEE THE OUPUT, OTHERWISE RETURN TO MESSAGE I/P FORM PAGE
   }
?>

您将需要一个Apache Web服务器,卷曲的支持。这些脚本正常工作在我的两个树莓派,我的Windows机器。在HTML / PHP可能有点对举步维艰的一面,因为我已经做了很多切割拿出我的数据库的东西,但它至少工作。我希望这是usful

You will need an Apache web server with curl support. These scripts work fine on both my raspberry pi and my Windows machine. The html/php may be a bit on the ropey side, as I've done a lot of cutting to take out my DB stuff, but it does at least work. I hope it's usful

这篇关于Android的推送服务,实现GCM服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆