消息推送机制原理

来源:互联网 发布:java 通过ip获取地址 编辑:程序博客网 时间:2024/06/06 20:53

在现在的android开发中,为了提高用户 的体验,我们会经常使用到消息推送这样一个功能,但是在一般的开发中我们通常采用极光推送等第三方的框架,来进行推送,那么google本身的推送的原理是什么呢?

首先我们可以定义一个布局文件里面含有两个按钮,其中一个按钮是启动,另一个是停止;具体的就不做详细的解答(相信大家都能做到这个功能),那么在MainActivity中的onCreate()里面设置两个按钮的点击事件,启动和停止具体的代码如下所示:

 @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        btnStart = (Button) findViewById(R.id.btnStart);        btnStop = (Button) findViewById(R.id.btnStop);        btnStart.setOnClickListener(this);        btnStop.setOnClickListener(this);    }    @Override    public void onClick(View v) {        int id = v.getId();        if (id == R.id.btnStart) {            // 启动Service            Intent intent = new Intent();            intent.setAction("zwk.MY_SERVICE");            startService(intent);        }        if (id == R.id.btnStop) {            // 关闭Service            Intent intent = new Intent();            intent.setAction("zwk.MY_SERVICE");            stopService(intent);        }    }    @Override    public void onBackPressed() {        System.exit(0);        super.onBackPressed();    }


以上是界面的相关逻辑,最主要的是在消息推送的服务类的逻辑的书写

书写一个类继承自service这个类,然后覆写其中的相关方法  onBindonStartCommand onDestroy

其中最主要的逻辑就是在onStartCommand中进行开启一个子线程,然后从服务端获取消息以后并且推送的逻辑,相关的代码如下所示:

    public IBinder onBind(Intent intent) {        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {
        // 初始化        messageNotification = new Notification();
        messageNotification.icon = R.drawable.ic_launcher;        messageNotification.tickerText = "新消息";        messageNotification.defaults = Notification.DEFAULT_SOUND;
        messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        messageIntent = new Intent(this, MainActivity.class);        messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0);        // 开启线程        messageThread = new MessageThread();        messageThread.isRunning = true;        messageThread.start();        return super.onStartCommand(intent, flags, startId);    }    /**     * 从服务器端获取消息     *      */    class MessageThread extends Thread {        // 设置是否循环推送        public boolean isRunning = true;        public void run() {            // while (isRunning) {            try {                // 间隔时间                Thread.sleep(1000);                // 获取服务器消息                String serverMessage = getServerMessage();                if (serverMessage != null && !"".equals(serverMessage)) {                    // 更新通知栏                    messageNotification.setLatestEventInfo(getApplicationContext(), "新消息", "您有新消息。" + serverMessage, messagePendingIntent);                    messageNotificatioManager.notify(messageNotificationID, messageNotification);                    // 每次通知完,通知ID递增一下,避免消息覆盖掉                    messageNotificationID++;                }            } catch (InterruptedException e) {                e.printStackTrace();            }            // }        }    }    @Override    public void onDestroy() {        // System.exit(0);        messageThread.isRunning = false;        super.onDestroy();    }    /**     * 模拟发送消息     *      * @return 返回服务器要推送的消息,否则如果为空的话,不推送     */    public String getServerMessage() {        return "NEWS!";    }


注意:在进行相关的操作的时候,千万不要忘记在mainfest.xml文件中进行设置注册!!


2 0