////////////////////2016/07/01////////////////////
///////////////////by xbw//////////////////////////
/////////////////环境 eclipse php ///////////
项目介绍:当A用户发送了一条动态后,B用户进行评论时,A用户将受到动态被评论的消息通知
应用截图:
项目准备:
1.信鸽推送,去官网注册,获取应用的key
2.信鸽推送服务器端demo(php 版),传送门
项目实现:
思路:
①我们在user_information数据表中新建一个设备token字段,我们需要在程序启动时,把设备token插入数据库中。
②当B用户对A用户做出评论时,A用户收到新消息通知。A用户对自己的动态做出评论A用户收不到消息通知。
实现:
①服务器端php操作数据库实现token插入。
<?php $response = array(); require("../db_config.php"); $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; mysql_query("set names 'utf8'"); mysql_select_db($mysql_database); if (isset($_POST['user_phone'])&&isset($_POST['user_token'])) { $user_phone = $_POST['user_phone']; $user_token = $_POST['user_token']; $sql="update user_information set user_token='$user_token' where user_phone='$user_phone'"; $result=mysql_query($sql); if ($result) { $response["success"] = 1; $response["message"] = "YES"; echo json_encode($response); } else { $response["success"] = 0; $response["message"] = "NO"; echo json_encode($response); } } else { $response["success"] = 0; $response["message"] = "Required field(s) is missing"; echo json_encode($response); } ?>
package com.example.mysql; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.widget.Toast; import com.example.mysql.AsyncTask_Change_Pwd.MysqlListenerss; public class AsyncTask_Change_Token extends AsyncTask<String, String, String> { JSONParser jsonParser = new JSONParser(); private static String url_up = Config_mysql.mysql_url_change_token; Context context; ProgressDialog dialog; public AsyncTask_Change_Token(Context context) { this.context = context; } public interface MysqlListenerss { public void Success(); public void Fail(); } private MysqlListenerss mysqlListener = null; public void setMysqlListenerss(MysqlListenerss mysqlListener) { this.mysqlListener = mysqlListener; } protected void onPreExecute() { super.onPreExecute(); dialog = Progress_Dialog.CreateProgressDialog(context); dialog.show(); } @SuppressWarnings("deprecation") @Override protected String doInBackground(String... p) { List<NameValuePair> params = new ArrayList<NameValuePair>(); String message = null; params.add(new BasicNameValuePair("user_phone", p[0])); params.add(new BasicNameValuePair("user_token", p[1])); try { JSONObject json = jsonParser .makeHttpRequest(url_up, "POST", params); message = json.getString("message"); } catch (Exception e) { e.printStackTrace(); message = "NONET"; } return message; } @SuppressLint("ShowToast") protected void onPostExecute(String message) { dialog.dismiss(); if (mysqlListener != null) { if (message.equals("YES")) { mysqlListener.Success(); } else { mysqlListener.Fail(); } } if (message.equals("NONET")) { // Toast.makeText(context, "网络连接失败", 8000).show(); } else if (message.equals("YES")) { // Toast.makeText(context, "修改成功", 8000).show(); } else { // Toast.makeText(context, "修改失败", 8000).show(); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedpreference = new SharedPreference(this); //Context context = getApplicationContext(); //XGPushManager.registerPush(context); XGPushManager.registerPush(this, new XGIOperateCallback() { @Override public void onSuccess(Object data, int flag) { AsyncTask_Change_Token changes = new AsyncTask_Change_Token( MainActivity.this); changes.setMysqlListenerss(new MysqlListenerss() { @Override public void Success() { } @Override public void Fail() { } }); changes.execute(sharedpreference.getID(), data + "");//data为token } @Override public void onFail(Object data, int errCode, String msg) { } }); Singleton.getInstance().setPhone(sharedpreference.getID()); MobclickAgent.onProfileSignIn(sharedpreference.getID()); download(); initView(); //savedInstanceState是记录onpause时刻的状态, //如果这状态非空,就不要新建fragment了 //if(savedInstanceState==null){ initFragment(); //} user(); }④获取token完成,评论推送完全在服务器端完成。当客户端向服务器端插入评论信息时,立即做出推送消息。
<?php $response = array(); require("../db_config.php"); $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; mysql_query("set names 'utf8'"); mysql_select_db($mysql_database); if (isset($_POST['user_phone']) && isset($_POST['pl_time'])&&isset($_POST['pl_content'])&&isset($_POST['pl_num'])) { $user_phone = $_POST['user_phone']; $pl_time = $_POST['pl_time']; $pl_content = $_POST['pl_content']; $pl_num = $_POST['pl_num']; $result = "INSERT INTO pl_information(user_phone,pl_time,pl_content,pl_num) VALUES('$user_phone', '$pl_time', '$pl_content', '$pl_num')"; mysql_query($result); if ($result) { $re=mysql_fetch_array(mysql_query("select user_phone from talk_information where talk_id='$pl_num'")); if($user_phone!=$re[0]){ $sqlsss="select user_token from user_information where user_phone='$re[0]'"; $rowssss = mysql_fetch_array(mysql_query($sqlsss)); $url="http://139.***.***.***/ecfun/mysql/push/push.php?id=1&token=".$rowssss[0];//推送用get请求完成 access_url($url); } $response["success"] = 1; $response["message"] = "YES"; echo json_encode($response); } else { $response["success"] = 0; $response["message"] = "NO"; echo json_encode($response); } } else { $response["success"] = 0; $response["message"] = "NO"; echo json_encode($response); } function access_url($url) { if ($url=='') return false; $fp = fopen($url, 'r') or exit('Open url faild!'); if($fp){ while(!feof($fp)) { $file.=fgets($fp).""; } fclose($fp); } return $file; } ?>⑤信鸽demo登场,好好利用一番。
<?php require_once ('XingeApp.php');//信鸽推送官网sdk下载的服务器端(php版)源码 if (isset($_GET['id'])&&isset($_GET['token'])){ $id = $_GET['id']; $token = $_GET['token']; var_dump(DemoPushSingleDeviceNotification($token,$id));<span style="font-family: Arial, Helvetica, sans-serif;">//单个设备下发通知消息</span> } //单个设备下发通知消息 function DemoPushSingleDeviceNotification($token,$id) { $push = new XingeApp(2100888888, '53653c1c694dedac8d8d8d8d8d8d8d8d');//在官网绑定的应用配置处查看 $mess = new Message(); $mess->setType(Message::TYPE_NOTIFICATION); $mess->setTitle("新消息通知"); if((int)$id==1){ $contentss='有人评论了你的动态'; }else if((int)$id==2){ $contentss='未开发通知'; } $mess->setContent($contentss); $mess->setExpireTime(86400); //$style = new Style(0); #含义:样式编号0,响铃,震动,不可从通知栏清除,不影响先前通知 $style = new Style(0,1,1,0,0); $action = new ClickAction(); $action->setActionType(ClickAction::TYPE_ACTIVITY); $action-> setActivity(); //$action->setIntent(); #打开url需要用户确认 $action->setComfirmOnUrl(1); $custom = array('key1'=>'value1', 'key2'=>'value2'); $mess->setStyle($style); $mess->setAction($action); $mess->setCustom($custom); $acceptTime1 = new TimeInterval(0, 0, 23, 59); $mess->addAcceptTime($acceptTime1); $ret = $push->PushSingleDevice($token, $mess); return($ret); }⑥差不多可以了,试试吧。
第一这么中规中矩的写,给个好评吧!!!!
转载标明出处:http://blog.csdn.net/xbw12138/article/details/51804911