尝试启动Android中的警报管理器以在某些条件下发送推送通知时遇到了一些问题。
所以基本上我要做的是我有一个事件列表,系统会发送关于事件的推送通知'前夕。这是我抓住事件日期并进行比较的部分:
for (int count = 0; count < _eventlist.size(); count++) {
Date alarmDate = null;
try {
alarmDate = dateFormat.parse(_eventlist.get(count)
.getEventDate());
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(alarmDate);
calendar.add(Calendar.DATE, -1);
Date alarmBeforeOneDay = calendar.getTime();
calendar.add(Calendar.DATE, 1);
if (dateFormat.format(alarmBeforeOneDay).equals(currentDate)) {
if (!AlarmInitialized(context)) {
notifyEventName = _eventlist.get(count).getEventName();
notifyEventTime = _eventlist.get(count).getEventTime();
notifyEventAddress = _eventlist.get(count).getEventAddress();
notifyEventPic = _eventlist.get(count).getEventPic();
scheduleAlarms(context);
}
}
}
如果今天是事件的前夕,我将执行scheduleAlarm()以启动广播接收器:
public static void scheduleAlarms(Context context) {
int day = Calendar.getInstance().get(Calendar.DATE);
int month = Calendar.getInstance().get(Calendar.MONTH);
int year = Calendar.getInstance().get(Calendar.YEAR);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh-mm");
String time = year + "-" + month + "-" + day + " " + "00" + "-" + "30";
Date dt = null;
try {
dt = df.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
long when = dt.getTime();
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, Alarm.class);
notificationCount = notificationCount + 1;
notificationIntent.putExtra("NotifyCount", notificationCount);
notificationIntent.putExtra("eventName", notifyEventName);
notificationIntent.putExtra("eventTime", notifyEventTime);
notificationIntent.putExtra("eventAddr", notifyEventAddress);
notificationIntent.putExtra("eventPic", notifyEventPic);
PendingIntent pi = PendingIntent.getBroadcast(context, 3,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP, when, pi);
}
所以基本上推送通知部分工作得很好。我收到了它。但是,推送通知仅在我启动活动时显示。
它不会根据我之前在方法中设置的时间发送。在这种情况下,如果今天是事件,它应该在上午12:30发送推送通知&#39前夕。但是,从我的代码来看,当它到达12:30 AM并且它没有发送。它只会在我运行Activity时发送。
任何指南/想法?提前谢谢。
答案 0 :(得分:0)
您是否尝试从此活动或任何服务发送通知?我的意思是执行scheduleAlarms(Context context)的上下文是什么?