2016-11-15 13 views
0

私は特定の日にいくつかのローカル通知をスケジュールしようとしています。私はそれらをスケジュールするためにAlarmManagerを使用しています。残念ながら、それは将来の時間には機能しません。なぜどんなアイデア?AlarmManagerが通知を配信しない

これは、私はアラームが

private long computeDelay(int day){ 

    long currentSystemTime = System.currentTimeMillis(); 

    Calendar scheduleTime = Calendar.getInstance(); 

    // used to compute number of hours and mins 
    int currentDay = scheduleTime.get(Calendar.DAY_OF_MONTH); 
    scheduleTime.set(Calendar.DAY_OF_MONTH, currentDay + day); 
    scheduleTime.set(Calendar.HOUR, 7); 
    scheduleTime.set(Calendar.MINUTE, 0); 
    scheduleTime.set(Calendar.SECOND, 0); 
    scheduleTime.set(Calendar.AM_PM, Calendar.PM); 

    return scheduleTime.getTimeInMillis(); 
} 

をトリガしなければならない時間を計算し、これは私がアラームをスケジュールする方法である方法です。

private void scheduleNotification(Notification notification, long time) { 
    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION"); 

    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
    notificationIntent.addCategory("android.intent.category.DEFAULT"); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); 
} 

コンテキストは、アプリケーション(つまり、getApplication())です。

多くの感謝!

+2

画像を削除してコードを貼り付けます。画像は検索できません。 – 323go

+1

ソート済み!それについて申し訳ありません! –

答えて

0

問題は、私がPendingIntentをどのように取得していたかにありました。

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

私はアラームスケジュール方法を数回呼んでいたし、要求コードは常に0ので、それが唯一の最後のアラームをスケジュールしていました。私は今、それぞれのアラームスケジュールにユニークなリクエストコードを与えています。うまくいくようです。

関連する問題