- 浏览: 311890 次
- 性别:
- 来自: 益阳
文章分类
最新评论
-
duckbit:
楼主是否能把共享layout的例子发给我,有点没明白 谢谢额! ...
Android换肤apk -
天涯海角262253:
...
Androidpn里的Xmpp的理解 -
lbstudy:
Activity之间的切换动画 -
dumbnesslys:
楼主可不可以给个源码 ,就build.xml的 772774 ...
Ant自动打包 -
finaljava:
build.xml 这么复杂,看看这个吧http://angr ...
Ant自动打包
XMPP(可扩展通讯和表示协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线探测。这个协议可能最终允许因特网用户向因特网上的其他任何人发送即时消息。用xmpp来实现android的push功能,感觉有点大材小用了,xmpp本身是一种即时通信协议。
xmpp是一种用于即时通信的协议,使用过程有点类似于我们使用QQ的过程,其使用过程分为三步:
1. 连接服务器,就好像打开QQ软件,看代码:
if(!mXmppManager.isConnected()) { ConnectionConfiguration config = new ConnectionConfiguration(mHost, mPort); config.setSecurityMode(SecurityMode.required); config.setSASLAuthenticationEnabled(false); config.setCompressionEnabled(false); XMPPConnection connection = new XMPPConnection(config); mXmppManager.setConnection(connection); try { connection.connect(); Log.i(LOGTAG, "XMPP connected successfully"); /** * 这个就是对于通信的xml文本进行解析的解析器,再把信息转换成IQ,这个相当于QQ的聊天信息 * 如果要用这个协议,其IQ的子类和IQProvider要进行重写 */ ProviderManager.getInstance().addIQProvider("notification", "androidpn:iq:notification", new NotificationIQProvider()); } catch (XMPPException e) { Log.d(LOGTAG, "the connection is error ... "); } mXmppManager.runTask(); } else { Log.i(LOGTAG, "XMPP connected already"); mXmppManager.runTask(); }
这一步主要是连接服务器,还有设置一些连接的参数,还有设置连接的解析器。
2. 如果没有用户,注册新的帐号和密码
只要连接了服务器了,客户端就可以向服务器端发送消息,发送是以Packet(数据包)来进行发送的,这个类有很多的子类,注册的子类为Registration。if(!mXmppManager.isRegistered()){
final String newUsername = newRandomUUID();
final String newPassword = newRandomUUID();
Registration registration = new Registration();
PacketFilter packetFilter = new AndFilter(new PacketIDFilter(
registration.getPacketID()), new PacketTypeFilter(
IQ.class));
PacketListener packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
// 服务器回复客户端
if(packet instanceof IQ) {
IQ response = (IQ) packet;
if(response.getType() == IQ.Type.ERROR) { // 注册失败
if (!response.getError().toString().contains(
"409")) {
Log.e(LOGTAG,"Unknown error while registering XMPP account! " + response.getError().getCondition());
}
} else if(response.getType() == IQ.Type.RESULT) { // 注册成功
mXmppManager.setUsername(newUsername);
mXmppManager.setPassword(newPassword);
// 把用户名和密码都保存到磁盘
Editor editor = mSharedPrefs.edit();
editor.putString(Contants.XMPP_USERNAME, newUsername);
editor.putString(Contants.XMPP_PASSWORD, newPassword);
editor.commit();
mXmppManager.runTask();
}
}
}
};
// 给注册的Packet设置Listener,因为只有等到正真注册成功后,我们才可以交流
mConnection.addPacketListener(packetListener, packetFilter);
registration.setType(IQ.Type.SET);
registration.addAttribute("username", newUsername);
registration.addAttribute("password", newPassword);
// 向服务器端,发送注册Packet包,注意其中Registration是Packet的子类
mConnection.sendPacket(registration);
} else { // 已经注册过了
mXmppManager.runTask();
}
还有要注意的是,上面的addPacketListener方法不是给所有发送的数据包设置listener,而只是针对这次的注册Packet。
3. 用注册的帐号和密码进行登陆(像用QQ号帐进行登陆一样)
// 判断是否已经登陆过了,是否是在登陆状态 if(!mXmppManager.isAuthenticated()) { try { mConnection.login(mUsername, mPassword, "AndroidpnClient"); // 设置XmppConnection的监听器 if(mXmppManager.getConnectionListener() != null) { mConnection.addConnectionListener(mXmppManager.getConnectionListener()); } // 设置服务器端推送的监听器 PacketFilter packetFilter = new PacketTypeFilter(NotificationIQ.class); PacketListener packetListener = mXmppManager.getNotificationPacketListener(); mConnection.addPacketListener(packetListener, packetFilter); mXmppManager.runTask(); } catch (XMPPException e) { // 登陆失败,应该重试 String INVALID_CREDENTIALS_ERROR_CODE = "401"; String errorMessage = e.getMessage(); // 如果只是因为没有注册,则进行重新注册 if (errorMessage != null && errorMessage.contains(INVALID_CREDENTIALS_ERROR_CODE)) { mXmppManager.reregisterAccount(); return; } mXmppManager.startReconnectionThread(); } catch (Exception e) { // 有可能mConnection都为空 Log.e(LOGTAG, "LoginTask.run()... other error"); Log.e(LOGTAG, "Failed to login to xmpp server. Caused by: " + e.getMessage()); mXmppManager.startReconnectionThread(); // 启动重连线程 } } else { mXmppManager.runTask(); }
这里设置了连接的监听器mConnection.addConnectionListener(),连接过程中有可以连接突然中断,连接出错等等问题,要进行监听。
设置服务器推送信息的Listener,接收到信息后,显示给用户。
如果出错的原因是401(无效的用户名和密码,则应该进行重新注册,再连接)
对于服务器push过来的信息进行处理,是在PacketListener类里面,这个接口里,只要实现一个方法processPacket(Packet packet),从传过来的Packet(数据包)里获取自己需要的数据:
对于Androidpn项目的整体代码分析,看下一遍文章。public void processPacket(Packet packet) {
if(packet instanceof NotificationIQ) {
NotificationIQ notification = (NotificationIQ) packet;
if(notification.getChildElementXML().contains("androidpn:iq:notification")) {
String notificationId = notification.getId();
String notificationApiKey = notification.getApiKey();
String notificationTitle = notification.getTitle();
String notificationMessage = notification.getMessage();
String notificationUri = notification.getUri();
Intent intent = new Intent(Contants.ACTION_SHOW_NOTIFICATION);
intent.putExtra(Contants.NOTIFICATION_ID, notificationId);
intent.putExtra(Contants.NOTIFICATION_API_KEY,notificationApiKey);
intent.putExtra(Contants.NOTIFICATION_TITLE,notificationTitle);
intent.putExtra(Contants.NOTIFICATION_MESSAGE, notificationMessage);
intent.putExtra(Contants.NOTIFICATION_URI, notificationUri);
mXmppManager.getContext().sendBroadcast(intent);
}
}
}
评论
Intent intent = getIntent();
String notificationId = intent
.getStringExtra(Constants.NOTIFICATION_ID);
String notificationApiKey = intent
.getStringExtra(Constants.NOTIFICATION_API_KEY);
String notificationTitle = intent
.getStringExtra(Constants.NOTIFICATION_TITLE);
String notificationMessage = intent
.getStringExtra(Constants.NOTIFICATION_MESSAGE);
String notificationUri = intent
.getStringExtra(Constants.NOTIFICATION_URI);
Log.i("test", "notificationId===============" + notificationId);
Log.i("test", "notificationApiKey============" + notificationApiKey);
Log.i("test", "notificationTitle============" + notificationTitle);
Log.i("test", "notificationMessage=================" + notificationMessage);
Log.i("test", "notificationDistance========distance=====111====" + notificationDistance);
Log.i("test", "notificationUri===============" + notificationUri);
疑惑1、 这几个参数代表什么意思呢, notificationId; notificationApiKey; notificationTitle; notificationMessage; notificationUri;
我看源码里好几处都用到这几个参数 ,
疑惑2、 现在服务端已经能将消息推送到服务端了并增加了一些参数,推送的消息里增加了距离distance等参数,应该在客户端的哪个类里获取这些值呢?
如何处理呢?
谢谢了
发表评论
-
浅析QQGame
2012-03-01 14:31 3399通过分析QQGame的项目,发现其存在两种方式: 1. 不安 ... -
opengl初探
2012-02-22 10:15 0android里的surfaceview Surfac ... -
onSaveInstanceState(Bundle outState)的调用时机
2012-02-15 11:10 3084Activity的方法onSaveInstanceState( ... -
Activity之间的切换动画
2012-02-15 10:53 22869从android系统2.1以后,android新增了方法:ov ... -
FLAG_ACTIVITY_NEW_TASK和affinity亲和力
2012-02-06 14:07 3566一直以为在intent里加了FLAG_ACTIVITY_N ... -
什么时候调用Dialog的dismiss()方法
2012-02-03 16:27 9967调用Dialog的dismiss()方法的方式: 1. 重写 ... -
引用主题属性
2011-12-28 18:09 2062文档里写的引用主题属性的方法如下: ?[<pac ... -
Intent的FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT
2011-12-18 14:50 54700Activity的两种启动模式:FLAG_ACTIVITY_C ... -
Android换肤apk
2011-12-11 15:30 4147在android系统中,每 ... -
Androidpn的框架浅析
2011-12-11 13:47 11735开发部署方法: 1. ... -
Log的tag的设置
2011-12-09 15:27 2381android输出日志的方法如下: int andro ... -
PopupWindow自适应布局
2011-12-04 18:24 13110Android自带的Menu菜单,常常无法满足我们的需求 ... -
Android程序换肤
2011-12-02 15:15 0Android的换肤功能,有多种方式,现在来说一下生成皮肤ap ... -
Activity与Service通信
2011-12-02 14:46 13053Activity与Service通信的方式有三种: 继 ... -
Ant自动打包2(打特殊厂商的包)
2011-11-29 15:08 4869由于公司内置的需要,我们的程序要针对不同的厂商打不同的ap ... -
Eclipse Indigo设置Courier New字体
2011-11-25 14:25 2151网上的教程如下: ... -
Fragment研究2
2011-11-24 10:43 6464几个类的结构的研 ... -
Ant自动打包
2011-08-23 15:56 38890Ant使用 在ant的官网http://ant. ... -
渐进式下载和流式下载有什么区别
2010-10-23 12:06 2260流式下载 下载边播放的BT软件,下载时必须要从电影的开头下 ... -
TableLaout样式
2010-10-12 10:41 1077要想用TableLaout来模仿九宫格的样式,其中有两个难点: ...
相关推荐
androidpn基于Xmpp的消息推送源码包含了完整的客户端和服务器端。androidpn客户端需要用到一个基于java的开源XMPP协议包asmack,这个包同样也是基于openfire下的另外一个开源项目smack,不过我们不需要自己编译,...
Androidpn推送框架源码分析及配置方法,压缩包里面包括服务端和客户端代码,及说明文档
Android例子源码androidpn基于Xmpp的消息推送
xmpp-androidPn的服务端和客户端打包
xmppPort=5222 是服务器的xmpp服务监听端口 运行androidpn-server-0.5.0\bin\run.bat启动服务器,从浏览器访问http://127.0.0.1:7070/index.do (androidPN Server有个轻量级的web服务器,在7070端口监听请求,接受...
基于XMPP协议、使用Androidpn服务器,Asmack编程的即时通讯IM客户端源代码;主要功能实现向客户端推送消息!
韩国著名的开源消息推送AndroidPN客户端,采用XMPP协议。
androidpn 加强版本 基于XMPP的安卓推送,里面有客户端和服务器版
androidpn xmpp 消息推送 供研究学习使用 项目相关的jar包 androidpn-client-0.5.0,androidpn-server-0.5.0-binmandroidpn-server-0.5.0-src
使用Apndroid Push Notification 实现android信息推送,AndroidPn项目是使用XMPP协议实现信息推送的一个开源项目。内涵服务端和客户端源码
为了测试androidpn 的服务端 写的模拟androidpn客户端的代码
将androidpn集成到我们平时开发的javaee中,直接启动tomcat就可以启动xmpp服务,不需要另外部署androidpn。 注释:项目有点大,所以没有上传jar包,可以直接使用项目中的maven下载所需jar文件
Androinpn是韩国人写的,这个是经过国人修改后的Androidpn项目。里面有修改人对代码的注释,更便于我们理解Androinpn,并且修复了一些原有的bug。
xmppPort=5222 是服务器的xmpp服务监听端口 运行androidpn-server-0.5.0\bin\run.bat启动服务器,从浏览器访问http://127.0.0.1:7070/index.do (androidPN Server有个轻量级的web服务器,在7070端口监听请求,接受...
XMPP(Android客户端的实现 _Tomcat版的Androidpn_).rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Androidpn客户端 和服务端源码下载,实现了消息推送。具体使用方法可百度。
androidpn为Android应用提供消息通知推送支持, 它本质上服务器端基于 Openfire,客户端基于 asmack,这二者都最 XMPP IM 开源实现里的二个基本组件,应该说 androidpn 只是把二者更多地结合起来用于做 Push的场景。...
androidpn 代码中实现了服务器重启后自动重连和离线消息推送和消息回执 android手机端的消息推送实现
亲测可用。Tomcat版的是基于mysql数据库的,搭建的时候mysql服务要打开,要建立相应的数据库。