2017-08-08 93 views
1

我在收到每日通知时遇到了一些麻烦。没有出现警报通知

我的应用程序获取用户希望提醒某事的时间,时间和分钟。

然后我用AlarmManager设立一个报警,此时重复,同时使用报警接收机,以创建通知。

我一直在尝试这个小时,但无法弄清楚我做错了什么。

我看了一堆其他SO问题,但没有人帮助过我。

我已经将用户的小时和分钟输入存储到日期对象get habitReminder中。

createNotifications()方法:

private void createNotifications() { 
    Log.i("Reminder at",""+habitReminder.getHours()+":"+habitReminder.getMinutes()); 
    //Create the alarms/notifications for the user 
    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    alarmIntent.putExtra("name", habitName); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 

    Log.i("createNotifications", "Alarm manager is created."); 

    //Set the timing of the reminder 
    Calendar calendar = Calendar.getInstance(); 
    Calendar now = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, habitReminder.getHours()); 
    calendar.set(Calendar.MINUTE, habitReminder.getMinutes()); 
    calendar.set(Calendar.SECOND,0); 

    //Check to make sure time is after the current date. 
    if(calendar.before(now)){ 
     calendar.add(Calendar.DATE, 1); 
    } 

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

    Log.i("createNotifications", "Alarm has been set for " +habitReminder.getHours()+":"+habitReminder.getMinutes() +" daily."); 
} 

我的报警接收器类:

public class AlarmReceiver extends BroadcastReceiver { 

private static int id =0; 

@Override 
public void onReceive(Context context, Intent intent) { 
    String name = intent.getStringExtra("name"); 
    String title = name + " Reminder!"; 
    String message = "Your reminder to keep up your habit!"; 
    long when = System.currentTimeMillis(); 
    Intent in = new Intent(context, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,in,PendingIntent.FLAG_CANCEL_CURRENT); 
    NotificationManager nM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification.Builder builder = new Notification.Builder(context) 
      .setContentIntent(contentIntent) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setAutoCancel(true) 
      .setWhen(when); 
    Notification notification = builder.build(); 
    nM.notify(id,notification); 
    id++; 
} 

}

而且我的Android manifest

<receiver android:name="com.closedbracket.trackit.AlarmReceiver" android:enabled="true"> 
</receiver> 

任何帮助真的很感激。

+0

你可以把'Log'里面你'的onReceive()'和检查,这是获得在设定的时间触发。 –

+0

我有一个'setExact(AlarmManagaer ...)'的例子,它适用于我,所以它可以帮助你,[创建和设置AlarmManager](https://stackoverflow.com/a/44303234/5212904),它还解释了如何从另一个Activity中取消AlarmManager。然后,如果你一直到达接收者,那么你已经完成了一半,问题是'Notification'是否被正确实现。 – ArtiomLK

回答

0

我认为你有论据setRepeating错误。第二个参数是第一个警报触发之前的时间间隔(抖动)。第三个是在第一次发生后续警报发生之后的间隔(间隔)。

您正在为AlarmManager.INTERVAL_DAY其中有86400000.你的报警值,每天将触发一次报警。

+0

是的,这是正确的,如果用户在上午8:30设置闹钟,我把这个时间作为第二个参数,并且每天重复。至少这就是我想要的,我做错了吗? – ZarifS

+0

在这种情况下,@ zelig63有正确的答案。系统可以将重复警报移动多达20%的时间间隔,如果睡着了,系统根本不会唤醒系统。使用'setAlarmClock'在确切的时间可靠地传送警报。你必须自己重置它...... –

1

如果你想有一个精确和可靠的报警,使用setAlarmClock。它会从电池中消耗更多的电量,但您确定闹铃会在设定的时间精确响起。

欲了解更多信息,可以参考Difference between setExact and setAlarmClock

+0

然而,我希望闹钟在这种情况下每天重复一次。因此,如果用户一次设置它,它将发送警报,这实际上只是一个通知,以便在当时提醒用户。 – ZarifS

+1

如果你想重复,你可以编程一个服务在闹钟时间执行,并在第二天触发一个新的闹钟。 – Zelig63