之前写了一个android小项目用到了ddpush推送,自己感觉很好用也很简单,没有友盟,极光等有附加的东西,相对jar包也小了很多.因为之前公司需要,也就是前端缺人我就零时去补了一下,好久没有写过前端了,好多框架都不会用了,反正忙的是焦头烂额,忙了差不多两个月最近稍微轻松了一点. 昨天是1024说是程序员节.但是
我连个码畜都不是啊,以后在也不和别人说是程序员了 ,就说搞电脑的好了.
上面的都是题外话.接下来介绍下ddpush和其使用方法.
上面的是ddpush的网址,我自己觉得ddpush的最大有点的jar包小,还有就是耗电量小.别的开发中没有明显感觉.
ddpush的官网 其实上面的东西想看就看下不看也没事.直接 找到下载
红色的区域下载下来
DDPush Server 是服务端需要配置的
Android的话只要下载安坐端App示例就行了,Android主要分为UDP和TCP,这个你要根据你项目的需求来选择.
TCP和DUP的最本质区别就是一个安全一个不安全,就是多一个校验,速度有点区别
TCP需要检验,也就是通俗的三次握手
UDP不不要
选着的时候要根据自己的具体要求选着 ,我之前的这个项目就是因为后端只支持TCP所以就用ddpush TCP
导入jar包 ,jar包在下载的Android案例代码里有
在libs里
然后把案例中的代码直接复制过来
package pad.android.grabsingle.ddpush.service;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import org.ddpush.im.v1.client.appuser.Message;
import org.ddpush.im.v1.client.appuser.TCPClientBase;
import pad.android.grabsingle.activity.ConstructionMain;
import pad.android.grabsingle.been.DDpushNotice;
import pad.android.grabsingle.api.SysConfig;
import pad.android.grabsingle.ddpush.Params;
import pad.android.grabsingle.ddpush.Util;
import pad.android.grabsingle.ddpush.receiver.TickAlarmReceiver;
import pad.android.grabsingle.utils.tools.AppUtil;
import pad.android.grabsingle.utils.tools.RxBus;
public class OnlineService extends Service {
protected PendingIntent tickPendIntent;
protected TickAlarmReceiver tickAlarmReceiver = new TickAlarmReceiver();
WakeLock wakeLock;
MyTcpClient myTcpClient;
Notification n;
private ConstructionMain constructionMain;
public static void reset(Context context) {
Intent intent = new Intent(context, OnlineService.class);
intent.putExtra("CMD", "RESET");
context.startService(intent);
}
public class MyTcpClient extends TCPClientBase {
public MyTcpClient(byte[] uuid, int appid, String serverAddr, int serverPort)
throws Exception {
super(uuid, appid, serverAddr, serverPort, 10);
}
@Override
public boolean hasNetworkConnection() {
return Util.hasNetwork(OnlineService.this);
}
@Override
public void trySystemSleep() {
tryReleaseWakeLock();
}
@Override
public void onPushMessage(Message message) {
if(message == null){
return;
}
if(message.getData() == null || message.getData().length == 0){
return;
}
if(message.getCmd() == 16){}// 0x10 通用推送信息
if(message.getCmd() == 17){}// 0x11 分组推送信息
if(message.getCmd() == 32){// 0x20 自定义推送信息
DDpushNotice dDpushNotice=new DDpushNotice();
dDpushNotice.notice=""+(Math.random()*100);
RxBus.getDefault().post(dDpushNotice);
}
setPkgsInfo();
}
}
public OnlineService() {
}
@Override
public void onCreate() {
super.onCreate();
this.setTickAlarm();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OnlineService");
resetClient();
}
@Override
public int onStartCommand(Intent param, int flags, int startId) {
if(param == null){
return START_STICKY;
}
String cmd = param.getStringExtra("CMD");
if(cmd == null){
cmd = "";
}
if(cmd.equals("TICK")){
if(wakeLock != null && wakeLock.isHeld() == false){
wakeLock.acquire();
}
}
if(cmd.equals("RESET")){
if(wakeLock != null && wakeLock.isHeld() == false){
wakeLock.acquire();
}
resetClient();
}
if(cmd.equals("TOAST")){
String text = param.getStringExtra("TEXT");
if(text != null && text.trim().length() != 0){
// Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}
setPkgsInfo();
return START_STICKY;
}
protected void setPkgsInfo(){
if(this.myTcpClient == null){
return;
}
long sent = myTcpClient.getSentPackets();
long received = myTcpClient.getReceivedPackets();
SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = account.edit();
editor.putString(Params.SENT_PKGS, ""+sent);
editor.putString(Params.RECEIVE_PKGS, ""+received);
editor.commit();
}
protected void resetClient(){
SharedPreferences account = this.getSharedPreferences(Params.DEFAULT_PRE_NAME, Context.MODE_PRIVATE);
String serverIp = SysConfig.webService_old_ip;
String serverPort = "9966";
String pushPort = "9999";
String userName = AppUtil.getSharedPreferenceValue(this, "username", "");
if(serverIp == null || serverIp.trim().length() == 0
|| serverPort == null || serverPort.trim().length() == 0
|| pushPort == null || pushPort.trim().length() == 0
|| userName == null || userName.trim().length() == 0){
return;
}
if(this.myTcpClient != null){
try{myTcpClient.stop();}catch(Exception e){}
}
try{
myTcpClient = new MyTcpClient(Util.md5Byte(userName), 1, serverIp, Integer.parseInt(serverPort));
myTcpClient.setHeartbeatInterval(50);
myTcpClient.start();
SharedPreferences.Editor editor = account.edit();
editor.putString(Params.SENT_PKGS, "0");
editor.putString(Params.RECEIVE_PKGS, "0");
editor.commit();
}catch(Exception e){
// Toast.makeText(this.getApplicationContext(), "操作失败:"+e.getMessage(), Toast.LENGTH_LONG).show();
}
// Toast.makeText(this.getApplicationContext(), "ddpush:终端重置", Toast.LENGTH_LONG).show();
}
protected void tryReleaseWakeLock(){
if(wakeLock != null && wakeLock.isHeld() == true){
wakeLock.release();
}
}
protected void setTickAlarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,TickAlarmReceiver.class);
int requestCode = 0;
tickPendIntent = PendingIntent.getBroadcast(this,
requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//小米2s的MIUI操作系统,目前最短广播间隔为5分钟,少于5分钟的alarm会等到5分钟再触发!2014-04-28
long triggerAtTime = System.currentTimeMillis();
int interval = 300 * 1000;
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, interval, tickPendIntent);
}
protected void cancelTickAlarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(tickPendIntent);
}
protected void cancelNotifyRunning(){
NotificationManager notificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
@Override
public void onDestroy() {
super.onDestroy();
//this.cancelTickAlarm();
cancelNotifyRunning();
this.tryReleaseWakeLock();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这个地方是设置接收service.ddpush推送的配置点
serverIp 配置服务器IP
serverPort 服务器端口号 默认是9966 (不用改)
pushPort 客户端端口号 默认9999(不用改)
onPushMessage 这个方法是接收ddpush推送的消息的
当返回码为16的时候 接收通用消息
当返回码为17的时候,接收分组消息
当返回码为32的时候是自定义消息
如果推送的消息只是短信提示的之类的话
但我这边是有特殊需求所以服务器用的是32的
其实ddpush的案例很详情了 ,处了这个OnlieService是主要执行的方法,别的是控制电量和Receiver广播通知ddpush启动的代码