android本地推送(退出app后依然可以)

时间:2021-01-01 23:44:10

1.小米手机无效,MIUI系统似乎改过android底层的一些东西


主要就是用到了AlarmManager,接着在AlarmReceiver实现notification 。  记得在manifest中添加AlarmReceiver即可

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);

Intent myIntent = new Intent(mContext, AlarmReceiver.class);
myIntent.putExtra("msg", msg);
myIntent.putExtra("id", count);
// 第二个参数一定不一样 否则重复只会让最后一个起作用
System.out.println("yzj id = " + count);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, count++, myIntent, 0);

alarmManager.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + time, pendingIntent);

附上github地址:https://github.com/playscforever/TestLocalNotification.git (里面的NotificationService.java是不需要的)