androidpn-client笔记及BUG修改

来源:互联网 发布:net stop mysql 编辑:程序博客网 时间:2024/05/07 02:29

这几天应业务需要,在搭建一个推送的DEMO。在参考了许多资料之后,最终使用了androidpn。

androidpn分server端和client端。server端几经折腾,最终采用了github上的一个版本,毕竟代码质量好控制。这将在另一篇文章详谈。

client端我是找到了一个精简版也改过一些BUG的版本,目前已经传到GITHUB上我fork的androidpn里,github地址:https://github.com/msdx/androidpn。在该项目中,我是使用androidstudio的,上面的androidpn-client-androidstudio目录就是了。下载可用。

目前上面已经的bug如下(如再有BUG修复会继续更新):

1、如果在客户端中配置的host不是IP而是域名时,无法连接。

解决方法是在getHost()方法中,判断是否为IP4或IP6的地址,如果不是,尝试将其当作域名来获取IP地址。修改 org.jivesoftware.smack.ConnectionConfiguration ,对getHost()方法修改如下:

    public String getHost() {        // 如果不是IP,则尝试将它以域名进行IP转换。        if(!InetAddressUtils.isIPv4Address(host) && !InetAddressUtils.isIPv6Address(host)) {            try {                InetAddress address = InetAddress.getByName(host);                host =  address.getHostAddress();            } catch (UnknownHostException e) {                Log.e(LOG_TAG, e.getMessage(), e);            }        }        return host;    }

2、如果重连接的任务已经完成,下次断开再重新连接时,等待时间重新开始计算。

在这里,我只是在ReconnectionThread类中的run()方法上加了一句waiting = 0;但是感觉好像不能这么改。未验证,有待确定。

另外,我修改了重连等待时间。这个就无所谓了,修改见:https://github.com/msdx/androidpn/commit/0d60a01134cdcd10c8be163887cab21255cc315a

3、第三个,就是修改了程序重新安装时会重新注册一个username的问题。

原来的username和password是随机生成的UUID,在这里我改为由手机设备号和包名生成的UUID,并将其作为username和password,涉及代码较多,见https://github.com/msdx/androidpn/commit/eb19ccf8abe549eb3b39002c12d2e60660bbcdc7。

主要修改org.androidpn.client.XmppManager的RegisterTask类的run()方法,代码如下:

public void run() {            Log.i(LOGTAG, "RegisterTask.run()...");            // 这个工具类是我参照网上的代码写的生成应用在设备里的唯一UUID的工具类。            final String uuid = UUIDUtil.getID(context);            if (!xmppManager.isRegistered()) {                // 密码也设成UUID,以使应用程序清除数据之后,再注册的用户username是一样的。                final String newUsername = uuid;                final String newPassword = uuid;                //这里代码省略。。。                     if (packet instanceof IQ) {                          IQ response = (IQ) packet;                         // 用于判断是否注册。如果已经注册过,会返回一个409错误(表示冲突)。                           String responseStr = null;                         if (response.getType() == IQ.Type.ERROR) {                                responseStr = response.getError().toString();                                if (!responseStr.contains("409")) {                                    Log.e(LOGTAG,                                            "Unknown error while registering XMPP account! "                                                    + response.getError()                                                    .getCondition()                                    );                                }                            }                            // 如果是注册冲突,表示已经注册过,也同样提交。原来的代码为else if (response.getType() == IQ.Type.RESULT ) {                            if (response.getType() == IQ.Type.RESULT || (responseStr != null && responseStr.contains("409"))) {               //以下省略。。。


4、修复当多条通知传送来时,点开后显示的都是最后一条的问题。

这里修改了org.androidpn.client.Notifier类,在notify()方法中将代码修改如下:

            // 主要是这里的requestCode必须不同。不一定要与下面的requestCode一样。            PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode,                    intent, PendingIntent.FLAG_UPDATE_CURRENT);            notification.setLatestEventInfo(context, title, message,                    contentIntent);            notificationManager.notify(requestCode, notification);            requestCode++;


5、修复锁屏后心跳包不发送,导致掉线问题。

这里修改了org/androidpn/client/NotificationService类,对其申请了电源锁。见:https://github.com/msdx/androidpn/commit/f6b1bf6da4a30dd712377a82d6e5b41ffc3d34f2


0 0