android 实现本地定时推送(兼容)
2018-05-03 本文已影响1843人
陨石坠灭
首先写几点感悟:
- 做兼容真的很累很费劲~
- android 8.0 广播部分不再支持动态注册,所以应该用service来实现定时推送功能
- 无论是闹钟还是通知,都得做兼容处理,android 8.0通知必须加channel_id,否则通知无法显示
- 查阅大量资料,发现代码都参差不齐,不过还是有很多值得参考的地方,目前这份代码有很多都是抄字那些博主的文章,然后稍加改动,加以整合而成
- 代码分为三个类,service类、闹钟工具类和通知工具类
首先,闹钟工具类:
package com.util;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import java.io.Serializable;
import java.util.Map;
/**
* 闹钟定时工具类
*/
public class AlarmTimerUtil {
/**
* 设置定时闹钟
*
* @param context
* @param alarmId
* @param action
* @param map 要传递的参数
*/
public static void setAlarmTimer(Context context, int alarmId, long time, String action,Map<String,Serializable> map) {
Intent myIntent = new Intent();
myIntent.setAction(action);
if(map != null){
for (String key: map.keySet()) {
myIntent.putExtra(key,map.get(key));
}
}
// PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);//如果是广播,就这么写
PendingIntent sender = PendingIntent.getService(context, alarmId, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //MARSHMALLOW OR ABOVE
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //LOLLIPOP 21 OR ABOVE
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(time, sender);
alarmManager.setAlarmClock(alarmClockInfo, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //KITKAT 19 OR ABOVE
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, sender);
} else { //FOR BELOW KITKAT ALL DEVICES
alarmManager.set(AlarmManager.RTC_WAKEUP, time, sender);
}
}
/**
* 取消闹钟
* @param context
* @param action
*/
public static void cancelAlarmTimer(Context context, String action,int alarmId) {
Intent myIntent = new Intent();
myIntent.setAction(action);
// PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);//如果是广播,就这么写
PendingIntent sender = PendingIntent.getService(context, alarmId, myIntent,0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(sender);
}
}
闹钟类,目前只用过AlarmManager.RTC_WAKEUP
类型,这个是精确定时,很多博客都提到过,不了解的可以自己查查。然后action
用来启动服务或者广播,alarmId
就是requestCode
,用来区别不同的闹钟。该工具类不仅仅可以用来定时通知,只要稍加改动,定时广播、定时任务、定时弹窗都是可以做的。
通知工具类
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import com.example.MainActivity;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
class NotifyObject implements Serializable{
public Integer type;
public String title;
public String subText;
public String content;
public String param;
public Long firstTime;
public Class<? extends Activity> activityClass;
@DrawableRes public int icon;
public List<Long> times = new ArrayList<>();
public static byte[] toBytes(NotifyObject obj) throws IOException{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
String content = null;
oos = new ObjectOutputStream(bout);
oos.writeObject(obj);
oos.close();
byte[] bytes = bout.toByteArray();
bout.close();
return bytes;
}
public static NotifyObject from(String content) throws IOException, ClassNotFoundException {
ByteArrayInputStream bin = new ByteArrayInputStream(content.getBytes("ISO-8859-1"));
ObjectInputStream ois = null;
NotifyObject obj = null;
ois = new ObjectInputStream(bin);
obj = (NotifyObject)ois.readObject();
ois.close();
bin.close();
return obj;
}
public static String to(NotifyObject obj) throws IOException{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
String content = null;
oos = new ObjectOutputStream(bout);
oos.writeObject(obj);
oos.close();
byte[] bytes = bout.toByteArray();
content = new String(bytes,"ISO-8859-1");
bout.close();
return content;
}
}
/**
* 消息通知工具
*/
public class NotificationUtil {
private static final String TAG = "NotificationUtil";
/**
* 通过定时闹钟发送通知
* @param context
* @param notifyObjectMap
*/
public static void notifyByAlarm(Context context,Map<Integer,NotifyObject> notifyObjectMap){
//将数据存储起来
int count = 0;
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Set<Integer> keySet = notifyObjectMap.keySet();
for (Integer key0: keySet) {
if(!notifyObjectMap.containsKey(key0)){
break;
}
NotifyObject obj = notifyObjectMap.get(key0);
if(obj == null){
break;
}
if(obj.times.size() <= 0){
if(obj.firstTime > 0){
try {
Map<String,Serializable> map = new HashMap<>();
map.put("KEY_NOTIFY_ID",obj.type);
map.put("KEY_NOTIFY",NotifyObject.to(obj));
AlarmTimerUtil.setAlarmTimer(context,++count,obj.firstTime,"TIMER_ACTION",map);
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
for (long time: obj.times) {
if(time > 0){
try {
Map<String,Serializable> map = new HashMap<>();
map.put("KEY_NOTIFY_ID",obj.type);
map.put("KEY_NOTIFY",NotifyObject.to(obj));
AlarmTimerUtil.setAlarmTimer(context,++count,time,"TIMER_ACTION",map);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
SharedPreferences mPreferences = context.getSharedPreferences("SHARE_PREFERENCE_NOTIFICATION",Context.MODE_PRIVATE);
SharedPreferences.Editor edit = mPreferences.edit();
edit.putInt("KEY_MAX_ALARM_ID",count);
edit.commit();
}
public static void notifyByAlarmByReceiver(Context context,NotifyObject obj){
if(context == null || obj== null)return;
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notifyMsg(context,obj,obj.type,System.currentTimeMillis(),manager);
}
/**
* 消息通知
* @param context
* @param obj
*/
private static void notifyMsg(Context context,NotifyObject obj,int nid,long time,NotificationManager mNotifyMgr){
if(context == null || obj == null)return;
if(mNotifyMgr == null){
mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
if(time <= 0)return;
//准备intent
Intent intent = new Intent(context,obj.activityClass);
if(obj.param != null && obj.param.trim().length() > 0){
intent.putExtra("param",obj.param);
}
//notification
Notification notification = null;
String contentText = obj.content;
// 构建 PendingIntent
PendingIntent pi = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//版本兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//兼容Android8.0
String id ="my_channel_01";
int importance =NotificationManager.IMPORTANCE_LOW;
CharSequence name = "notice";
NotificationChannel mChannel =new NotificationChannel(id, name,importance);
mChannel.enableLights(true);
mChannel.setDescription("just show notice");
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});
mNotifyMgr.createNotificationChannel(mChannel);
Notification.Builder builder = new Notification.Builder(context,id);
builder.setAutoCancel(true)
.setContentIntent(pi)
.setContentTitle(obj.title)
.setContentText(obj.content)
.setOngoing(false)
.setSmallIcon(obj.icon)
.setWhen(System.currentTimeMillis());
if(obj.subText != null && obj.subText.trim().length() > 0){
builder.setSubText(obj.subText);
}
notification = builder.build();
}else if (Build.VERSION.SDK_INT >= 23) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(obj.title)
.setContentText(contentText)
.setSmallIcon(obj.icon)
.setContentIntent(pi)
.setAutoCancel(true)
.setOngoing(false)
.setWhen(System.currentTimeMillis());
if(obj.subText != null && obj.subText.trim().length() > 0){
builder.setSubText(obj.subText);
}
notification = builder.build();
} else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
Notification.Builder builder = new Notification.Builder(context);
builder.setAutoCancel(true)
.setContentIntent(pi)
.setContentTitle(obj.title)
.setContentText(obj.content)
.setOngoing(false)
.setSmallIcon(obj.icon)
.setWhen(System.currentTimeMillis());
if(obj.subText != null && obj.subText.trim().length() > 0){
builder.setSubText(obj.subText);
}
notification = builder.build();
}
if(notification != null){
mNotifyMgr.notify(nid, notification);
}
}
/**
* 取消所有通知 同时取消定时闹钟
* @param context
*/
public static void clearAllNotifyMsg(Context context){
try{
NotificationManager mNotifyMgr =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancelAll();
SharedPreferences mPreferences = context.getSharedPreferences("SHARE_PREFERENCE_NOTIFICATION",Context.MODE_PRIVATE);
int max_id = mPreferences.getInt("KEY_MAX_ALARM_ID",0);
for (int i = 1;i <= max_id;i++){
AlarmTimerUtil.cancelAlarmTimer(context,"TIMER_ACTION",i);
}
//清除数据
mPreferences.edit().remove("KEY_MAX_ALARM_ID").commit();
}catch (Exception e){
Log.e(TAG,"取消通知失败",e);
}
}
}
-
NotifyObject
实现了序列化,便于传递参数,然后Activity
类换成自己的Activity就行了。 -
clearAllNotifyMsg
用于清除所有通知,同时清除所有闹钟。 -
notifyByAlarmByReceiver
无论是在广播还是在服务中,都可以调用这个进行立即通知 -
notifyByAlarm
在activity中调用改方法,将开启定时通知 -
notifyMsg
这个是真正实现通知的方法,但并不需要外部调用
服务或者广播类及其配置
服务和广播配置一个就可以了,目前我才有的是服务的配置方法
服务的写法
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.io.IOException;
public class AlarmService extends Service {
public AlarmService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String str = intent.getStringExtra("KEY_NOTIFY");
if(str == null || str.trim().length() == 0)return super.onStartCommand(intent, flags, startId);
try {
NotifyObject obj = NotifyObject.from(str);
NotificationUtil.notifyByAlarmByReceiver(this,obj);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
}
服务的配置方法:
<service
android:name=".service.AlarmService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="TIMER_ACTION" />
</intent-filter>
</service>
广播的写法
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.util.NotificationUtil;
import java.io.IOException;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("TIMER_ACTION".equals(intent.getAction())){
String str = intent.getStringExtra("KEY_NOTIFY");
NotifyObject obj = null;
try {
obj = NotifyObject.from(str);
if(obj != null){
NotificationUtil.notifyByAlarmByReceiver(context,obj);//立即开启通知
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
广播的配置方法:
<receiver android:name=".service.AlarmReceiver">
<intent-filter>
<action android:name="TIMER_ACTION" />
</intent-filter>
</receiver>
由于查看的资料太多了,所以就不一一列举了,然后提供一个测试方法,可以在MainActivity
的OnCreate
方法中调用:
long now = System.currentTimeMillis();
long interval[] = {0,10,60,3000,6000,12000,30000,50000,60000,100000};
int count = 1;
SimpleDateFormat smf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
for (long inter:interval) {
Date date = new Date(now+inter);
NotifyObject obj = new NotifyObject();
...
/**
type:count++,
title:"标题",
subText:"理论提醒时间:"+smf.format(date),
content:"类型:"+(count-1)+","+inter,
"",
firstTime:now+inter,
-1l,
null
//赋值就自己赋值啦~
**/
...
notifyObjects.put(obj.type,obj);
}
NotificationUtil.notifyByAlarm(this,this.notifyObjects);