2017-05-30 45 views
0

我有它設置一個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永遠不會被解僱(我把一個斷點檢查)。

編輯

這是我從onHandleIntentNotificationService

@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

你registerd清單中的接收器? – MatPag

+0

@MatPag yes:' ' – Clyky

+0

你應該嘗試用這個[這裏]替換你的AlarmManager代碼(https://stackoverflow.com/a/31838628/) 2910520)並查看它是否有效。通過這種方式,我們可以肯定地說,問題是你的代碼的Intent或AlarmManager,而不是別的 – MatPag

回答

1

NotificationPublisher只對因同一requestCodelast迭代,你報警PendingIntent使用解僱。

由於您正在使用requestCode作爲0所有報警,它只是覆蓋了先前的報警data。這就是爲什麼只有last報警你的NotificationPublisher的onReceive()執行。

SOLUTION:

使用不同的報警PendingIntent不同REQUEST_CODE

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

希望這將有助於〜

+0

非常感謝你,我已經瞭解了哪裏問題是:)但我有另一個問題:我已經解決了(我認爲)一個殘酷的方式,使用隨機'生成隨機ID的問題。這是對的嗎?那麼,是否有另一種發送一組通知的巧妙方式? – Clyky

+0

我認爲沒有其他辦法。主要的是你必須爲每個鬧鐘設置唯一的請求代碼。爲了發出多重警報,你應該使用循環技術。 – FAT

+0

如果不可能同時觸發多個警報,則可以重複創建一個警報,而不是創建多個警報。希望你明白:) – FAT