Android系统消息推送
2017-08-10 15:31
393 查看
现在好多应用都接入了推送功能,市面上也有很多关于推送的第三方,例如极光等等,那么我们需求不大,接入极光会造成很大的资源浪费,下面我们来看下利用android服务进行本地推送消息,
1.注册一个Service
2.在AndroidMan中注册
3.由于我是需要全局应用就在Application中进行启动了
4.也可以配合服务端使用,定时推送消息
1.注册一个Service
package com.baofoo.manshenghuo.ui.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 com.baofoo.manshenghuo.MainActivity; import com.baofoo.manshenghuo.R; 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; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { myThread = new MyThread(); myThread.start(); super.onCreate(); } @Override public 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 ; @Override public void run() { while (true){ c = Calendar.getInstance(); if(c.get(Calendar.HOUR_OF_DAY) == 15){ 4000 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.也可以配合服务端使用,定时推送消息
相关文章推荐
- [置顶] JPush PhoneGap/ Cordova Plugin插件Android系统上不能接收推送消息的问题?
- Android MPush开源消息推送系统:简洁、安全、支持集群
- Android与iOS系统的消息推送机制
- android系统下消息推送机制
- android学习之六·是用系统nofity实现系统消息推送
- Android 自定义锁屏 监听系统消息推送
- Android与iOS系统的消息推送机制
- [置顶] JPush PhoneGap/ Cordova Plugin插件Android系统上不能接收推送消息的问题?
- 通过推送消息控制Android系统锁屏、唤醒
- android 消息推送
- php实现简单消息发送+极光推送系统
- Android 基于Netty的消息推送方案之Hello World(一)
- Android主线程的消息系统(Handler\Looper)
- Chromium on Android: Android在系统Chromium为了实现主消息循环分析
- Android推送通知消息管理
- Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒
- iOS和Android使用MQTT协议实现消息推送和即时通讯(替代XMPP,已经有TMS MQTT)
- Android消息推送方案
- Android 使用极光推送自定义消息打造个性的消息推送效果
- Android (Notification)消息推送机制