现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息,

1.注册一个Service

import android.annotation.TargetApi;
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.os.Build;
import android.os.IBinder;
import java.util.Calendar;/*** Created by 70883 on 2017/8/10.*/
public class PushSmsService extends Service {private NotificationManager manager;private PendingIntent pi;private MyThread myThread;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {myThread = new MyThread();myThread.start();super.onCreate();}@Overridepublic void onDestroy() {super.onDestroy();}@TargetApi(Build.VERSION_CODES.JELLY_BEAN)private void notification() {// 获取系统的通知管理器  manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(getApplicationContext(),MainActivity.class);pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);Notification notification = new Notification.Builder(getApplicationContext()).setAutoCancel(true).setContentText("工作在忙,也要吃饭哦").setContentIntent(pi).setSmallIcon(R.mipmap.ic_icon).setWhen(System.currentTimeMillis()).build();notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置,比如铃声、震动、闪灯  notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失  notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除  manager.notify(0, notification);}private class  MyThread  extends Thread{private Calendar  c ;@Overridepublic void run() {while (true){c = Calendar.getInstance();if(c.get(Calendar.HOUR_OF_DAY) == 15){try {notification();sleep(1000*60*60);} catch (InterruptedException e) {e.printStackTrace();}}}}}
}

2.在AndroidMan中注册

<service android:name=".ui.Service.PushSmsService"></service>  

3.由于我是需要全局应用就在Application中进行启动了

 public void startService() {Intent intent = new Intent(this, PushSmsService.class);// 启动服务startService(intent);}

4.也可以配合服务端使用,定时推送消息

Android系统消息推送相关推荐

  1. unity android本地推送,Unity安卓本地推送

    https://blog.csdn.net/macaopark/article/details/69053691 新建推送脚本:LocalReceive import android.annotati ...

  2. Android消息推送完美解决方案全析

    Android消息推送完美解决方案全析 参考文章: (1)Android消息推送完美解决方案全析 (2)https://www.cnblogs.com/xueshui20/p/4151405.html ...

  3. Android 消息推送

    Android 消息推送 那位大哥做过啊 android是用服务做吧? pushsharp 雲推送 你要有服務器來服務 不是云推送吧 去搜這個,有server,有client,有demo 自己做的话, ...

  4. Android 服务器推送技术

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  5. Android之基于xmpp openfire smack开发之Android消息推送技术原理分析和实践[4]

    http://blog.csdn.net/shimiso/article/details/8156439 前面几篇给大家系统讲解的有关xmpp openfire smack asmack相关的技术和使 ...

  6. android实现推送方式解决方案,Android实现推送方式解决方案系列教程

    Android实现推送方式解决方案系列教程 1-5 Android实现推送方式解决方案系列教程 总结者:难民 交流Q群: 137824028 Android实现推送方式解决方案系列之一--XMPP协议 ...

  7. 总结的太牛了,android实时推送!

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  8. android 统一推送平台,工信部实验室成立安卓统一推送联盟:推送服务将实现统一...

    据微信公众号" 泰尔终端实验室"7月19日消息,移动互联网时代,消息推送是移动应用(APP)的一项重要功能,目前中国安卓系统生态环境尚不成熟,设备碎片化现象严重,导致不同应用与操作 ...

  9. Android谷歌推送

    1.    简介 Android谷歌推送GCM(全称Google Cloud Messaging),谷歌云通讯,它可以实现消息从服务器端推送到android系统相关的应用上,从而实现服务器主动推送消息 ...

最新文章

  1. final阶段团队贡献分分配
  2. 「SVN」Linux下svn使用命令
  3. 在OpenCV环境下对图像做Gamma校正
  4. 「数据ETL」从数据民工到数据白领蜕变之旅(三)-除了Excel催化剂之外PowerQuery新物种同样值得期待...
  5. pythondocx模板_使用python-docx-template修改word文档
  6. msf各种弱口令爆破
  7. 接口与事件之图形界面的认证登录
  8. 初次注册使用Tushare Pro报错问题
  9. 软件工程需求分析文档模板
  10. SIP协议及与Freeswitch的关系
  11. win10环境下python3如何使用PyV8
  12. 100名网工备考IE,最终能通过的,到底有几个?
  13. OpenGL教程——windows安装openGL
  14. The Beatles Strawberry Fields Forever 歌词翻译
  15. 从JDBC到Mybatis
  16. 前端框架UMI3基础知识和快速上手
  17. 木纹标识lisp_AutoLisp学习笔记:变量类型
  18. PyImport_ImportModule
  19. 学校机房网上作业发放、提交系统案例
  20. opencv python 多张jpg图片转成avi视频

热门文章

  1. AcWing Round #14
  2. 线程同步互斥机制--互斥锁
  3. UNR #1 火车管理
  4. 如何合并svn分支到主干上
  5. Java violate变量
  6. 面试蚂蚁金服(意外拿到offer)分享四面经历,从线程锁到数据库
  7. php spry文本域_SPRY验证文本域之用户名称
  8. PFC的数据类型及命名规则
  9. linux中给普通用户添加root用户权限
  10. 链接h5代码_H5到底是什么?看完你就明白了!