如何做安卓 推送推送

Android平台上最主流的几种消息推送方案进行分析和对比
本文主旨在于,对目前Android平台上最主流的几种消息推送方案进行分析和对比,比较客观地反映出这些推送方案的优缺点,帮助大家选择最合适的实施方案。
方案1、使用GCM服务(Google
Cloud Messaging)
简介:Google推出的云消息服务,即第二代的C2DM。
优点:Google提供的服务、原生、简单,无需实现和部署服务端。
缺点:Android版本限制(必须大于2.2版本),该服务在国内不够稳定、需要用户绑定Google帐号,受限于Google。
方案2、使用XMPP协议(Openfire
+ Spark + Smack)
简介:基于XML协议的通讯协议,前身是Jabber,目前已由IETF国际标准化组织完成了标准化工作。
优点:协议成熟、强大、可扩展性强、目前主要应用于许多聊天系统中,且已有开源的Java版的开发实例androidpn。
缺点:协议较复杂、冗余(基于XML)、费流量、费电,部署硬件成本高。
方案3、使用MQTT协议(更多信息见:)
简介:轻量级的、基于代理的“发布/订阅”模式的消息传输协议。
优点:协议简洁、小巧、可扩展性强、省流量、省电,目前已经应用到企业领域(参考:),且已有C++版的服务端组件rsmb。
缺点:不够成熟、实现较复杂、服务端组件rsmb不开源,部署硬件成本较高。
方案4、使用HTTP轮循方式
简介:定时向HTTP服务端接口(Web Service API)获取最新消息。
优点:实现简单、可控性强,部署硬件成本低。
缺点:实时性差。
对各个方案的优缺点的研究和对比,推荐使用MQTT协议的方案进行实现,主要原因是:MQTT最快速,也最省流量(固定头长度仅为2字节),且极易扩展,适合二次开发。接下来,我们就来分析使用MQTT方案进行Android消息的原理和方法,并架设自己的推送服务。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。android通过服务实现消息推送
这里运用到的andorid知识模块主要有Notification和Service,和一个android-async-http-master开源框架
android项目中,有时会有这样一种需求:客户每隔一段时间,就像服务器发送一个请求,以获取某些重要的、实时更新的消息。比如天气预报。
如何让应用实现在后台一直处于运行状态,并且每个一段时间就向服务器发一个请求?android里的四大之一:服务,就为我们提供了这种功能。
因此,我们可以尝试在服务里边定义一个线程,只要服务不停止,线程就一直在运行,让它每隔一段时间,就向服务器发送一个请求。
我们来看一下核心代码
1.在Activity中,我们可以通过startService()来启动服务
[java] view plaincopy在CODE上查看代码片派生到我的代码片
public void open(View view) { &
& & & & Intent intent = new Intent(this, PushSmsService.class); &
& & & & // 启动服务 &
& & & & startService(intent); &
2.这里我们需要自定义一个服务类,去继承android的Service类
&* 短信推送服务类,在后台长期运行,每个一段时间就向服务器发送一次请求&
&* @author jerry&
public class PushSmsService extends Service { &
& & private MyThread myT &
& & private NotificationM &
& & private Notif &
& & private PendingI &
& & private AsyncHttpC &
& & private boolean flag = &
& & @Override &
& & public IBinder onBind(Intent intent) { &
& & & & // TODO Auto-generated method stub &
& & @Override &
& & public void onCreate() { &
& & & & System.out.println(&oncreate()&); &
& & & & this.client = new AsyncHttpClient(); &
& & & & this.myThread = new MyThread(); &
& & & & this.myThread.start(); &
& & & & super.onCreate(); &
& & @Override &
& & public void onDestroy() { &
& & & & this.flag = &
& & & & super.onDestroy(); &
& & private void notification(String content, String number, String date) { &
& & & & // 获取的通知管理器 &
& & & & manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); &
& & & & notification = new Notification(R.drawable.ic_menu_compose, content, &
& & & & & & & & System.currentTimeMillis()); &
& & & & notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯 &
& & & & notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失 &
& & & & notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除 &
& & & & Intent intent = new Intent(getApplicationContext(), &
& & & & & & & & ContentActivity.class); &
& & & & intent.putExtra(&content&, content); &
& & & & intent.putExtra(&number&, number); &
& & & & intent.putExtra(&date&, date); &
& & & & pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); &
& & & & &&
& & & & notification.setLatestEventInfo(getApplicationContext(), number &
& & & & & & & & + &发来短信&, content, pi); &
& & & & // 将消息推送到状态栏 &
& & & & manager.notify(0, notification); &
& & private class MyThread extends Thread { &
& & & & @Override &
& & & & public void run() { &
& & & & & & String url = &http://110.65.99.66:8080/jerry/PushSmsServlet&; &
& & & & & & while (flag) { &
& & & & & & & & System.out.println(&发送请求&); &
& & & & & & & & try { &
& & & & & & & & & & // 每个10秒向服务器发送一次请求 &
& & & & & & & & & & Thread.sleep(10000); &
& & & & & & & & } catch (InterruptedException e) { &
& & & & & & & & & & e.printStackTrace(); &
& & & & & & & & } &
& & & & & & & & // 采用get方式向服务器发送请求 &
& & & & & & & & client.get(url, new AsyncHttpResponseHandler() { &
& & & & & & & & & & @Override &
& & & & & & & & & & public void onSuccess(int statusCode, Header[] headers, &
& & & & & & & & & & & & & & byte[] responseBody) { &
& & & & & & & & & & & & try { &
& & & & & & & & & & & & & & JSONObject result = new JSONObject(new String( &
& & & & & & & & & & & & & & & & & & responseBody, &utf-8&)); &
& & & & & & & & & & & & & & int state = result.getInt(&state&); &
& & & & & & & & & & & & & & // 假设偶数为未读消息 &
& & & & & & & & & & & & & & if (state % 2 == 0) { &
& & & & & & & & & & & & & & & & String content = result.getString(&content&); &
& & & & & & & & & & & & & & & & String date = result.getString(&date&); &
& & & & & & & & & & & & & & & & String number = result.getString(&number&); &
& & & & & & & & & & & & & & & & notification(content, number, date); &
& & & & & & & & & & & & & & } &
& & & & & & & & & & & & } catch (Exception e) { &
& & & & & & & & & & & & & & e.printStackTrace(); &
& & & & & & & & & & & & } &
& & & & & & & & & & } &
& & & & & & & & & & @Override &
& & & & & & & & & & public void onFailure(int statusCode, Header[] headers, &
& & & & & & & & & & & & & & byte[] responseBody, Throwable error) { &
& & & & & & & & & & & & Toast.makeText(getApplicationContext(), &数据请求失败&, 0) &
& & & & & & & & & & & & & & & & .show(); &
& & & & & & & & & & } &
& & & & & & & & }); &
& & & & & & } &
& & & & } &
注意我们要在清单文件中注册Service
&service android:name=&.PushSmsService&&&/service& &
由于通知信息使用到了手机的震动功能和网络访问,所以需要配置权限
&uses-permission android:name=&android.permission.VIBRATE&/& &
&uses-permission android:name=&android.permission.INTERNET&/& &
我们再定义一个Activity,用于用户点击下拉通知栏里的某条消息后,跳转到详细的消息界面
public class ContentActivity extends Activity { &
& & @Override &
& & protected void onCreate(Bundle savedInstanceState) { &
& & & & super.onCreate(savedInstanceState); &
& & & & setContentView(R.layout.activity_content); &
& & & & Intent intent = getIntent(); &
& & & & TextView tv_content = (TextView) this.findViewById(R.id.tv_content); &
& & & & TextView tv_number = (TextView) this.findViewById(R.id.tv_number); &
& & & & TextView tv_date = (TextView) this.findViewById(R.id.tv_date); &
& & & & if (intent != null) { &
& & & & & & String content = intent.getStringExtra(&content&); &
& & & & & & String number = intent.getStringExtra(&number&); &
& & & & & & String date = intent.getStringExtra(&date&); &
& & & & & & tv_content.setText(&内容:& + content); &
& & & & & & tv_number.setText(&号码:& + number); &
& & & & & & tv_date.setText(&日期:& + date); &
& & & & } &
同样,我们需要在清单文件中注册新的Activity
&activity android:name=&.ContentActivity&&&/activity& &
注意到上边的代码是需要服务器提供支持的,我们去新建一个简单的服务器,里边添加一个Servlet和一个实体类就可以了
public class PushSmsServlet extends HttpServlet { &
& & private static int index = 1; &
& & public void doGet(HttpServletRequest request, HttpServletResponse response) &
& & & & & & throws ServletException, IOException { &
& & & & System.out.println(index); &
& & & & // List&Sms& smsList = new ArrayList&Sms&(); &
& & & & Sms sms = new Sms(index, &
& & & & & & & & &我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你我爱你& + index, &&, &
& & & & & & & & &&); &
& & & & index++; &
& & & & // smsList.add(sms); &
& & & & // sms = new Sms(0, &我真的爱你&, &&, &&); &
& & & & // smsList.add(sms); &
& & & & // sms = new Sms(1, &我真的真的爱你&, &&, &&); &
& & & & // smsList.add(sms); &
& & & & response.setContentType(&text/html&); &
& & & & request.setCharacterEncoding(&UTF-8&); &
& & & & response.setCharacterEncoding(&utf-8&); &
& & & & PrintWriter out = response.getWriter(); &
& & & & Gson gson = new Gson(); &
& & & & // 将Sms类的数据转换为json数据格式 &
& & & & String json = gson.toJson(sms); &
& & & & out.write(json); &
& & & & out.flush(); &
& & & & out.close(); &
& & public void doPost(HttpServletRequest request, HttpServletResponse response) &
& & & & & & throws ServletException, IOException { &
& & & & this.doGet(request, response); &
再定义封装用于存放消息的类
public class Sms { &
& & private S &
& & private S &
& & private S &
& & public Sms(int state, String content, String date, String number) { &
& & & & super(); &
& & & & this.state = &
& & & & this.content = &
& & & & this.date = &
& & & & this.number = &
这里只贴出核心代码14:34 提问
Android service怎么在后台一直接收推送
最近在做几个推送的demo,像百度云推送、极光推送,它们都是demo中集成后,可以正常接收推送,
即使我们把程序退出,或者长按Home键清空最近程序后依旧可以接收。
但是我自己用xmpp,参考开源的项目实现的Android端接收推送时,正常打开程序和
关闭程序时都是可以收到推送,如果长按Home键清空程序的话,就收不到程序了,好像是
后台接收推送的service被清掉了,但是我的service是startService开启的,配置文件中的
process属性也设置的“:message”,这样还是不行,求解???
按赞数排序
你要增加服务的复活几率,也就是网上常说的杀不死的服务,但是绝对杀不死是不可能的,你可以接收一些系统广播,然后启动服务,现在常用的推送服务都是这样
在配置文件中启动服务,可以参考极光推送的demo
搞一个BroadCastRecviver,接收系统的ACTION_TIME_TICK,每次收到之后检查你服务是否在运行,如果停止运行就启动它。
搞一个BroadCastRecviver,接收系统的ACTION_TIME_TICK,每次收到之后检查你服务是否在运行,如果停止运行就启动它。
尽量保证你的service“活着”~~
你这种情况是正常的,总之是尽量保活你的Service,至于第三方的Service如何保活的,这个属于机密技术,如果你不会,只能参考网上的方法去提高复活几率
其他相似问题

我要回帖

更多关于 sony z5 安卓7.0 推送 的文章

 

随机推荐