推送sdk (类似百度 推送 ) android mina (待优化... )
本demo 研究的是原理,不要计较太多。。。。 至于token生成,service 保活,定时启动 不做说明。
下载:http://download.csdn.net/download/knight_black_bob/9823413
双开模拟器 进行验证。。。。
package cn.baoyou.curiousby.com.imsdk.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; import com.alibaba.fastjson.JSON; import org.apache.mina.core.session.IoSession; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import cn.baoyou.curiousby.com.imsdk.core.ClientSocket; import cn.baoyou.curiousby.com.imsdk.core.ServerSocket; import cn.baoyou.curiousby.com.imsdk.core.callback.IRecieveCallBack; import cn.baoyou.curiousby.com.imsdk.message.Message; import cn.baoyou.curiousby.com.imsdk.session.SessionManager; /** * Created by cmcc-B100036 on 2017/4/18. */ public class MinaService extends Service{ public static boolean send(Message msg){ Log.i("baoy","--------------------"); if (SessionManager.getInstance().isConnectable()){ try { Log.i("baoy","write start" + msg.toString()); SessionManager.getInstance().write(msg); Log.i("baoy","write end"); return true; } catch (Exception e) { return false; } } return false; } private IRecieveCallBack iRecieveCallBack = new IRecieveCallBack() { @Override public void recieve(Message msg) { Log.i("baoy","recievie msg ==== service " + msg.toString()); Intent intent=new Intent(); intent.setAction("cn.baoyou.curiousby.com.imsdk.msgrecieve"); intent.putExtra("recieveMsg", JSON.toJSONString( msg)); intent.putExtra("recieveMsgBytes",JSON.toJSONString( msg).getBytes()); sendBroadcast(intent); } }; public final class MinaBinder extends Binder { public MinaService getService() { return MinaService.this; } } @Nullable @Override public IBinder onBind(Intent intent) { return null; } ClientSocket instance = null; ServerSocket instancee = null; @Override public void onCreate() { super.onCreate(); ExecutorService service = Executors.newFixedThreadPool(2); service.execute(new Runnable() { @Override public void run() { instance = ClientSocket.getInstance(); instance.setiRecieveCallBack(iRecieveCallBack); instance.init(); IoSession ioSession = null; try { instance.start(); }catch (Exception e) { } } }); service.execute(new Runnable() { @Override public void run() { instancee = ServerSocket.getInstance(); instancee.init(); IoSession ioSession = null; try { instancee.start(); }catch (Exception e) { } } }); service.shutdown(); } @Override public void onDestroy() { super.onDestroy(); instance.stop(); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { super.onRebind(intent); } }
package cn.baoyou.curiousby.com.imsdk.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.provider.SyncStateContract; import android.util.Log; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import cn.baoyou.curiousby.com.imsdk.constants.Constants; import cn.baoyou.curiousby.com.imsdk.message.Message; import cn.baoyou.curiousby.com.imsdk.message.MessageFactory; import cn.baoyou.curiousby.com.imsdk.operater.IMOriginalCallBack; /** * Created by cmcc-B100036 on 2017/4/19. */ public class MsgReciever extends BroadcastReceiver { private IMOriginalCallBack iMOriginalCallBack; private static Map<Class,IMOriginalCallBack> map = new HashMap<>(); public MsgReciever setRecieve(Class clazz ,IMOriginalCallBack iMOriginalCallBack) { this.iMOriginalCallBack = iMOriginalCallBack; MsgReciever.map.put(clazz,iMOriginalCallBack); return this; } @Override public void onReceive(Context context, Intent intent) { if(Constants.ACTION .equals(intent.getAction())){ String recieveMsg = intent.getStringExtra("recieveMsg"); Log.i("baoy","=========msgrecieve==================="+recieveMsg); Message msg = MessageFactory.build(recieveMsg); for ( Map.Entry<Class,IMOriginalCallBack> entry : map.entrySet()) { Log.i("baoy","=========msgrecieve callback==================="+msg); Log.i("baoy","=========msgrecieve callback==================="+entry.getKey()); Log.i("baoy","=========msgrecieve callback==================="+entry.getValue()); ((IMOriginalCallBack) entry.getValue()).recieve(msg); } } } }
package cn.baoyou.curiousby.com.imsdk.core; import android.util.Log; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; import cn.baoyou.curiousby.com.imsdk.core.callback.IRecieveCallBack; import cn.baoyou.curiousby.com.imsdk.core.callback.IReconnect; import cn.baoyou.curiousby.com.imsdk.message.ConnectionMessage; import cn.baoyou.curiousby.com.imsdk.message.Message; import cn.baoyou.curiousby.com.imsdk.message.MessageFactory; import cn.baoyou.curiousby.com.imsdk.message.ShortMessage; import cn.baoyou.curiousby.com.imsdk.message.enums.MessageTag; import cn.baoyou.curiousby.com.imsdk.session.SessionManager; public class ClientHandler extends IoHandlerAdapter { // private static Logger log = Logger.getLogger(ClientHandler.class); private IRecieveCallBack iRecieveCallBack; private IReconnect iReconnect; volatile static boolean isConnected = false; public ClientHandler(IReconnect iReconnect,IRecieveCallBack iRecieveCallBack) { this.iRecieveCallBack = iRecieveCallBack; this.iReconnect = iReconnect; } @Override public void sessionOpened(IoSession session) throws Exception { Log.i("baoy","session created opened"); super.sessionOpened(session); } @Override public void sessionCreated(IoSession session) throws Exception { Log.i("baoy","session created start"); super.sessionCreated(session); Log.i("baoy","session created end"); isConnected = true; buildConnection(session); } @Override public void sessionClosed(IoSession session) throws Exception { Log.i("baoy","session created closed"); super.sessionClosed(session); isConnected = false; } @Override public void messageSent(IoSession session, Object message) throws Exception { Log.i("baoy","session created sent"); super.messageSent(session, message); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { Log.i("baoy","session created caught"); super.exceptionCaught(session, cause); isConnected = false; } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { Log.i("baoy","session created idle"); super.sessionIdle(session, status); } @Override public void messageReceived(IoSession session, Object json) throws Exception { super.messageReceived(session, json); Log.i("baoy","recievie msg 00 === (" + json.toString() +")MessageFactory.isChecked(json.toString()"+MessageFactory.isChecked(json.toString()) ); if(MessageFactory.isChecked(json.toString()).equals( MessageTag.msg)) { Log.i("baoy","recievie msg 01 === " ); Message build = MessageFactory.build(json.toString()); if (iRecieveCallBack != null) { Log.i("baoy", "recievie msg 1 ===" + json.toString()); iRecieveCallBack.recieve(build); Log.i("baoy", "recievie msg 2 ===" + json.toString()); } } } public void buildConnection(IoSession session){ if(session.isConnected()) { ConnectionMessage msg = new ConnectionMessage(); msg.setUserKey("2"); msg.setJson("connect"); session.write(com.alibaba.fastjson.JSON.toJSONString(msg)); } } }
package cn.baoyou.curiousby.com.imsdk.core; import android.util.Log; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.core.filterchain.IoFilter; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.service.IoConnector; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketConnector; import java.net.InetSocketAddress; import java.nio.charset.Charset; import cn.baoyou.curiousby.com.imsdk.codec.CodecFactory; import cn.baoyou.curiousby.com.imsdk.constants.Constants; import cn.baoyou.curiousby.com.imsdk.core.callback.IRecieveCallBack; import cn.baoyou.curiousby.com.imsdk.core.callback.IReconnect; import cn.baoyou.curiousby.com.imsdk.message.ConnectionMessage; import cn.baoyou.curiousby.com.imsdk.service.MinaService; public class ClientSocket implements IReconnect{ IoConnector connector=null; private ClientSocket(){} private static ClientSocket instance = new ClientSocket(); public static ClientSocket getInstance(){ return instance; } private static IRecieveCallBack iRecieveCallBack; public static void setiRecieveCallBack(IRecieveCallBack iRecieveCallBack) { ClientSocket.iRecieveCallBack = iRecieveCallBack; } public void init(){ connector = new NioSocketConnector(); ClientHandler handler = new ClientHandler(this,iRecieveCallBack); connector.setHandler(handler); IoFilter filter = new ProtocolCodecFilter(new CodecFactory()); connector.getFilterChain().addLast("codec", filter); connector.getFilterChain().addLast("logging", new LoggingFilter()); } public boolean start(){ ConnectFuture connect = connector.connect(new InetSocketAddress(Constants.CLIENT_SOCKET_ADDRESS,Constants.CLIENT_SOCKET_PORT)); connect.awaitUninterruptibly(); return true; } public void stop(){ if(connector != null){ connector.dispose(); } } @Override public void reconnection() { while(true) { if (!ClientHandler.isConnected) { start(); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package cn.baoyou.curiousby.com.imorginal; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import cn.baoyou.curiousby.com.imsdk.broadcastreceiver.MsgReciever; import cn.baoyou.curiousby.com.imsdk.message.Message; import cn.baoyou.curiousby.com.imsdk.message.ShortMessage; import cn.baoyou.curiousby.com.imsdk.operater.IMOriginalCallBack; import cn.baoyou.curiousby.com.imsdk.service.MinaService; public class MainActivity extends AppCompatActivity implements View.OnClickListener ,IMOriginalCallBack{ private EditText sendText; private TextView recieveText; private MsgReciever reciever = new MsgReciever().setRecieve(MainActivity.class , this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(getApplicationContext(), MinaService.class); this.startService(intent); this.findViewById(R.id.bt_send_button).setOnClickListener(this); recieveText = (TextView) this.findViewById(R.id.id_recieveMsg); sendText = (EditText) this.findViewById(R.id.id_sendmsg); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_send_button: Log.i("baoy","=========================send"+sendText.getText().toString()); ShortMessage shortMessage = new ShortMessage(); shortMessage.setUserKey("2"); shortMessage.setForm("2"); shortMessage.setTo("1"); shortMessage.setJson(sendText.getText().toString()); blackboard += ""+((ShortMessage)shortMessage ).getForm()+"对" +((ShortMessage)shortMessage ).getTo() +" 说:"+shortMessage.getJson() +"\n"; recieveText.setText(blackboard); MinaService.send(shortMessage); Log.i("baoy","========================"); break; } } static String blackboard = ""; @Override public void recieve(Message msg) { Log.i("baoy","=====main activity callback===================" + msg); blackboard += ""+((ShortMessage)msg ).getForm()+"对" +((ShortMessage)msg ).getTo() +" 说:"+msg.getJson() +"\n"; recieveText.setText(blackboard); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!
相关推荐
1基于蓝牙的项目开发--蓝牙温度监测器.docx
AppDynamics:性能瓶颈识别与优化
xtrabackup银河麒麟v10rpm安装包
2024年全球产品经理大会(脱敏)PPT合集,共34份。 1、AI 原生产品设计的 7 个反共识 2、AI 时代的策略产品与内容社区推荐实践 3、AI时代的用户界面设计 4、AI智能陪练:大模型赋能销售成长 5、AI浪潮中的应用主义者 6、AI驱动下的B端产品的思考与创新 7、AI驱动业务增长的探索与实践 8、Al Native 生产力工具的发展、价值与商业落地 9、B端产品设计避坑指南 10、GenAl驱动的xGen电商AI平台产品实践与思考 11、Kwaipilot 在快手的落地实践 12、OPPO AI的探索新交互到新生态 13、RPA + AI打造大模型驱动的领先数字员工 14、产品AI化重塑的思考与实践 15、产品分析:通过关键指标助力团队与企业成功 16、从RPA到Al Agent,高价值、可落地的智能助手 17、从流量运营到AI驱动的机器增长 18、做穿越时代的产品 19、创造好工具,创造世界一流产品力 20、医疗健康场景的大模型产品探索 21、即时零售柔性供应链体系建设与AIGC在零售数字化的探索 22、向量数据库的出海实践与未来展望 23、大模型在B端落地思考实践
基于物联网技术的停车场智能管理系统设计用户有单独APP
Adobe XD:AdobeXD高级技巧与最佳实践.docx
ARKit(iOS的增强现实):ARKit的多人AR场景实现
1python自动化脚本.docx
河北省、市、区县及街镇可编辑SVG图
金融工程之量化交易算法:均值回归:时间序列分析与预测.docx
技术资料分享ADV7123非常好的技术资料.zip
Sawmill_cn.ppt
使用LabVIEW输入数字n,然后计算n的阶乘
1无人值守灌溉系统--stm.docx
金融工程之量化交易算法:动量交易:金融数据获取与处理.docx
基于SSM的毕业设计源码
头像图片调试使用用来调试
GITS_sawmill8.5.8.1_x64_linux-ubuntu11.tar.gz
Newspaper 是一个专为新闻、杂志和内容丰富网站设计的 WordPress 主题。它非常适合博客、在线出版和内容展示,具备多种强大功能。以下是 Newspaper 的主要特点: 响应式设计:确保网站在各种设备上(手机、平板、桌面)都能良好显示,优化用户体验。 丰富的预建模板:提供多个专业设计的预建布局和页面模板,用户可以快速导入并根据需求进行修改。 强大的页面构建器:内置的 TagDiv Composer 允许用户通过拖放功能轻松创建和自定义页面,无需编写代码。 SEO 优化:主题经过优化,有助于提升网站在搜索引擎中的排名,增加流量。 多种内容展示选项:提供多种文章格式和布局选项,如网格、列表、视频和画廊,方便展示不同类型的内容。 社交媒体集成:支持社交媒体分享功能,帮助用户轻松与观众互动并提高网站的曝光度。 实时自定义:支持实时预览功能,用户可以在更改设置时即时查看效果。 持续更新和支持:定期更新主题,确保用户获得最新功能和安全性,同时提供专业的技术支持。
国内市场:功能化脂质市场现状研究分析与发展前景预测报告(2024版).docx