我有一项设置AlarmManager
的服务,如下所示:
private void setupInsuranceNotification(@NonNull String plate, @NonNull LocalDate insuranceDeadline)
{
LocalDate now = LocalDate.now();
int daysToDeadline = getDaysBetweenDates(now, insuranceDeadline);
String notificationTitle = "Assicurazione " + plate;
String notificationMessage;
int millsBetweenNotifications = 1000 * 60 * 60 * 24; // 1 day default
LocalDate notificationStart = now;
if (daysToDeadline > 0)
{
notificationMessage = daysToDeadline + " giorni alla scadenza.";
if (daysToDeadline > 30)
{
// show the notification only when they are 30 days left
notificationStart = now.withFieldAdded(DurationFieldType.days(), daysToDeadline - 30);
}
}
else if (daysToDeadline == 0)
{
notificationMessage = "Assicurazione scaduta oggi.";
}
else
{
if (daysToDeadline >= -15)
{
// tolerance period
notificationMessage = Math.abs(daysToDeadline) + " alla fine del periodo di tolleranza!";
millsBetweenNotifications = 1000 * 60 * 60 * 6; // 6 hours
}
else
{
// insurance expired
notificationMessage = "Periodo di tolleranza finito! L'assicurazione è scaduta!";
millsBetweenNotifications = 1000 * 60 * 60; // 1 hour
}
}
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra("notification_title", notificationTitle);
notificationIntent.putExtra("notification_message", notificationMessage);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationStart.toDate().getTime(), millsBetweenNotifications, alarmIntent);
}
这是我的NotificationPublisher
课程:
public class NotificationPublisher extends BroadcastReceiver
{
private static final int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent)
{
String title = intent.getStringExtra("notification_title");
String message = intent.getStringExtra("notification_message");
Notification.Builder builder = new Notification.Builder(context);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setSmallIcon(R.drawable.ic_notification);
manager.notify(NOTIFICATION_ID, builder.build());
}
}
我的问题是setupInsuranceNotification
被正确执行直到结束(没有任何异常),但我的NotificationPublisher
永远不会被解雇(我已经设置了一个断点来检查)
修改
这是来自onHandleIntent
班级的NotificationService
:
@Override
protected void onHandleIntent(@Nullable Intent intent)
{
Debug.waitForDebugger();
if (Utilities.vehicleFileExists(this))
{
try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(Utilities.getVehicleListFile(this));
Element root = doc.getDocumentElement();
root.normalize();
NodeList vehicleNodeList = root.getElementsByTagName("vehicle");
for (int i = 0; i < vehicleNodeList.getLength(); i++)
{
Element vehicleElement = (Element) vehicleNodeList.item(i);
Element matriculationDateElement = (Element) vehicleElement.getFirstChild();
Element endInsuranceDateElement = (Element) matriculationDateElement.getNextSibling();
Element lastInspectionDateElement = (Element) endInsuranceDateElement.getNextSibling();
String plate = vehicleElement.getAttribute("plate");
LocalDate matriculationDate = LocalDate.parse(matriculationDateElement.getFirstChild().getNodeValue(), DateTimeFormat.forPattern(Utilities.Formats.Date.ITALIAN));
LocalDate endInsuranceDate = LocalDate.parse(endInsuranceDateElement.getFirstChild().getNodeValue(), DateTimeFormat.forPattern(Utilities.Formats.Date.ITALIAN));
LocalDate lastInspectionDate = null;
if (lastInspectionDateElement.getFirstChild() != null) // <inspection_dates/>
{
lastInspectionDate = LocalDate.parse(lastInspectionDateElement.getFirstChild().getNodeValue(), DateTimeFormat.forPattern(Utilities.Formats.Date.ITALIAN));
}
LocalDate inspectionDeadline = getInspectionDeadline(matriculationDate, lastInspectionDate);
setupInsuranceNotification(plate, endInsuranceDate);
setupInspectionNotification(plate, inspectionDeadline);
}
}
catch (ParserConfigurationException | SAXException | IOException ignored) {}
}
}
答案 0 :(得分:0)
由于您在警报NotificationPublisher
中使用的last
requestCode
,PendingIntent
仅针对requestCode
次迭代触发。
当您使用0
作为data
进行所有警报时,它只会覆盖之前的警报last
。这就是为什么只有onReceive()
警告您的NotificationPublisher REQUEST_CODE
已执行。
<强> SOLUTION:强>
对不同的警报PendingIntent
使用不同的PendingIntent alarmIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
。
library(dplyr)
User <- c(1,1,1,2,3,3)
Date <- c("1/1/17","1/1/17","1/2/17","1/1/17","1/1/17","1/2/17")
df <- data.frame(User,Date,stringsAsFactors = FALSE)
df <- df %>%
group_by(User, Date) %>%
mutate(Unique = if_else(duplicated(Date) == FALSE, 1, 0))
希望这会有所帮助〜