2017-08-08 19 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++; 
} 

}

そして、私のアンドロイドmanifest

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

どんな助けも本当に感謝します。

+0

を参照することができます。 –

+0

私は 'setExact(AlarmManagaer ...)'の例を持っています。それは私のために役立ちますので、[AlarmManagerの作成と設定](https://stackoverflow.com/a/44303234/5212904)、それ'ActivityManager'から' AlarmManager'を取り消す方法についても説明します。それで受信機に届いたら、半分の方法で済んでいて、 'Notification'が正しく実装されているかどうかという疑問があります。 – ArtiomLK

答えて

0

あなたには、setRepeatingという引数が間違っていると思います。 2番目の引数は、最初のアラームが鳴るまでの時間間隔(ジッタ)です。 3番目は、後続のアラームが発生する最初の発生後の間隔(間隔)です。

AlarmManager.INTERVAL_DAYの値が86400000のアラームを設定しています。アラームは1日に1回発生します。

+0

はい、正解です。ユーザーが午前8時30分にアラームを設定した場合、その時刻を第2引数として置き、毎日繰り返しています。少なくとも私が行っていたことは、間違っているのですか? – ZarifS

+0

その場合、@ zelig63には正しい答えがあります。システムは、間隔の20%も繰り返すアラームを移動することができ、眠っている場合はシステムをまったく復帰させないことがあります。 'setAlarmClock'を使用すると、正確な時刻に確実にアラームを届けることができます。あなたはそれを自分でリセットする必要があります... –

1

正確で信頼性の高いアラームが必要な場合は、setAlarmClockを使用してください。バッテリーの電力がより多く消費されますが、設定された時間にアラームが正確に鳴ることを確信しています。詳細については

、あなたがあなたの `onReceive(内部` Log`)を入れ、 `、それは、設定された時間に起動なっているかどうかを確認することができますDifference between setExact and setAlarmClock

+0

この場合、毎日アラームを繰り返す必要があります。したがって、ユーザーが一度に設定すると、アラームを送信します。このアラームは、実際にその時の何かをユーザーに思い出させる通知です。 – ZarifS

+1

繰り返しを希望する場合は、アラーム時刻に実行するようにサービスをプログラムし、翌日に新しいアラームをトリガーするようにすることができます。 – Zelig63

関連する問題