mqtt实现向手机推送消息问题总结
2015-11-06 09:32
447 查看
网上有很多介绍采用mqtt实现手机推送消息的应用,主要是调用ibm推出的wmqtt.jar包。具体实现过程就不多说了,下载程序网上也有很多。根据网友提供的网址http://download.csdn.net/detail/csh159/5115797下载了程序代码。
android端搭建环境如下:
成功搭建环境后,导入工程,出现问题如下:
1、编译提示错误:
2、运行提示警告:MqttException: NULL,手机一直连接不上服务器
以上两个问题的原因均为android sdk版本不合适导致。解决方案:
问题1:
关于Notification类,不同Android版本的使用方法网上有介绍:
低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。
高 于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在 notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。
高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。
所以,查看自己android sdk版本是多少,修改对应的代码段。
问题2:
网上有很多解决方法是AndroidManifest.xml文件中将选择Min SDK Version 低于 10 ,并且不要填写 Target/Max SDK Version ,或者Target Version版本低于10, project.properties文件中的版本控制这当前编译时调用的 android 支持包的版本。
但是试了很多次均不管用,后来找到一种方法成功解决了问题:在调用wmqtt.jar之前,即在进行MQTTConnection连接之前加上下面代码即可。
android端搭建环境如下:
成功搭建环境后,导入工程,出现问题如下:
1、编译提示错误:
<span style="font-size:14px;">The method setLatestEventInfo(PushService, String, String, PendingIntent) is undefined for the type Notification</span>
2、运行提示警告:MqttException: NULL,手机一直连接不上服务器
以上两个问题的原因均为android sdk版本不合适导致。解决方案:
问题1:
关于Notification类,不同Android版本的使用方法网上有介绍:
低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。
<span style="font-size:14px;">Intent intent = new Intent(this,MainActivity); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); notification.setLatestEventInfo(context, title, message, pendingIntent); manager.notify(id, notification); </span>
高 于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在 notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。
<span style="font-size:14px;">Notification.Builder builder = new Notification.Builder(context) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true); notification=builder.getNotification(); </span>
高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。
<span style="font-size:14px;">Notification notification = new Notification.Builder(context) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .build(); </span>
所以,查看自己android sdk版本是多少,修改对应的代码段。
问题2:
网上有很多解决方法是AndroidManifest.xml文件中将选择Min SDK Version 低于 10 ,并且不要填写 Target/Max SDK Version ,或者Target Version版本低于10, project.properties文件中的版本控制这当前编译时调用的 android 支持包的版本。
但是试了很多次均不管用,后来找到一种方法成功解决了问题:在调用wmqtt.jar之前,即在进行MQTTConnection连接之前加上下面代码即可。
<span style="font-size:14px;">if(android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); }</span>
相关文章推荐
- 找不到Qt5Cored.dll(Release和Debug版连接了不同的库)
- Qt读取ANSI格式文件——利用QTextCodec将其他编码格式转换为Unicode格式
- QtCreator调试传入运行参数
- Qt Creator快捷键大全,附快捷键配置方法
- Qt for Windows:使用WinPcap开发高性能UDP服务器
- qt 主窗口中排列多个子窗口的布局问题
- 天嵌开发版 imx6 移植qt
- QT5入门之32-QCustomPlot
- 开发板在qt上环境搭建
- Qt创建first工程报错Circular <- first dependency dropped
- Qt报错 /usr/bin/ld: error: cannot find -lGL
- Qt启动画面(类似Office)
- Pyqt实现无边框窗口拖动及改变窗口大小
- 每日一笔记之2:QT之坐标系统:
- qt如何用指定程序打开当前路径下的文件
- Ubuntu下 嵌入式Qt开发环境的搭建
- 使用Qt开发中国象棋(六):将军和死棋检测
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- QT笔记:数据库总结(一)
- QT5入门之31-QT QTreeWidget