2017-03-09 118 views
0

我試圖讓我的每日通知服務正常工作,但我遇到了問題。在應用程序啓動時觸發Android通知

我應該只在當天在未來特定時間收到通知,但我注意到,

if (time_that_i_set_for_notification < current_time_of_the_day) notification triggers at the boot of my app 

這是錯誤的,因爲在這種條件下,該通知應觸發第二天,而不是在我啓動應用程序的時候。

這裏是我試圖讓事情工作:

我把這個在我的MainActivity,onCreate()方法

private void scheduleNotification(int hour, int minute){ 
    Intent notificationIntent = new Intent(this, NotificationReceiver.class); 
    notificationIntent.putExtra("notifId", notifId); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); 

// Set the alarm to start at approximately at a time 
     DatePicker datePicker = new DatePicker(this); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); 
     if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 
     calendar.set(Calendar.HOUR_OF_DAY, 12); 
     calendar.set(Calendar.MINUTE, 12); 

     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
// With setInexactRepeating(), you have to use one of the AlarmManager interval 
// constants--in this case, AlarmManager.INTERVAL_DAY. 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 

我認爲加入if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);我已經取得的成果我需要。

謝謝你,祝你有美好的一天。

回答

0

在致電setInexactRepeating,添加此檢查:

if (calendar.getTimeInMillis() < System.currentTimeMillis()) 
    calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY); 

希望這有助於。

0

好吧有一個錯誤的檢查,如果你改變它,你應該能夠解決你的問題。

這裏,我們去:

Intent notificationIntent = new Intent(this, NotificationReceiver.class); 
    notificationIntent.putExtra("notifId", notifId); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); 

// Set the alarm to start at approximately at a time 
     DatePicker datePicker = new DatePicker(this); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); 
     //if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 
     calendar.set(Calendar.HOUR_OF_DAY, 14); 
     calendar.set(Calendar.MINUTE, 50); 
     if (calendar.getTimeInMillis() < System.currentTimeMillis()) calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY); 
     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
// With setInexactRepeating(), you have to use one of the AlarmManager interval 
// constants--in this case, AlarmManager.INTERVAL_DAY. 

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, pendingIntent); 

我評論//if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); ,因爲它應該是不必要的。

讓我知道它是否工作。

UPDATE:您應該完全檢查處理您的每日通知的服務是否高消耗。高排水電池應用程序會打擾您的用戶。

+1

完美!我解決了這個問題!非常好的答案謝謝! – Claff

相關問題