2017-08-08 124 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