2016-10-10 10 views
0

私は特定のタスクを実行する必要があることを示す月の25日ごとに通知を受け取るAndroidアプリ用の関数を作成したいと考えています。Android:特定の日付に通知メッセージをスケジュールする

私は、次のコード使用して通知を表示することができました。今、このシステムは唯一、私は私のアプリを開いているときに動作し、私はこれを表示することはできません

public class NotificationPublisher extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    long[] pattern = {0, 300, 0}; 
    PendingIntent pi = PendingIntent.getActivity(context,, intent, 0); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.small_logo_ico) 
      .setContentTitle(context.getResources().getString(R.string.notification_title)) 
      .setContentText(context.getResources().getString(R.string.notification_content)) 
      .setVibrate(pattern) 
      .setAutoCancel(true); 

    mBuilder.setContentIntent(pi); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(, mBuilder.build()); 
} 

}

をアプリが閉じられたとき。 Android notification at specific date これを試した後(スケジュール部分)、受信者の登録を解除する際にエラーが発生するので、アプリを終了すると動作しないことに気付きました(登録を解除する)受信者がキャンセルされ、通知が表示されません。スケジュールのために使用さ

コード:

NotificationPublisher receiver = new NotificationPublisher(); 
    this.receiver = receiver; 
    IntentFilter filter = new IntentFilter("ALARM_ACTION"); 
    registerReceiver(receiver, filter); 

    Intent intent = new Intent("ALARM_ACTION"); 
    intent.putExtra("param", "My scheduled action"); 
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0); 
    // I choose 15s after the launch of my application 
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, operation) ; 

私が欠けている、または私は特定の日付の通知をスケジュールするために間違った方法を使用しています何がありますか? (現在の通知は、将来15秒間スケジュールされるように設定されています。これはテストのためのものです。特定の日付にこれを表示する機能が用意されています)

+1

http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time – HAXM

答えて

0

これは、 。たぶんあなたは下のコードから得ることができます。 「alarmManager.setRepeating(:私は最後の部分を変更した

Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.DAY_OF_MONTH, 15); 
     calendar.set(Calendar.HOUR_OF_DAY, 12); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MILLISECOND, 0); 
     if (calendar.getTimeInMillis() < System.currentTimeMillis()) { 
      calendar.add(Calendar.DAY_OF_YEAR, 30); 
     } 
     Intent myIntent = new Intent(getApplicationContext(), MyReceiver.class); 
     myIntent.putExtra("NOTI_MSG",getString(R.string.notification_sidas)); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), NOTI_REQ_CODE_SIDAS, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY * 30, pendingIntent); 
    } 
+0

AlarmManager.RTC、System.currentTimeMillis()+ 3000、 2000、pendingIntent); "残りの部分を同じように保ちました(もちろん、必要に応じて参照を変更しています)。これがうまくいかなかった理由は何ですか? – Gerton

関連する問題