2017-03-09 75 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

相关问题