Я создал уведомление с AlarmManager, но получаю уведомление только в тот момент, когда создаю сигнал тревоги. Как я могу решить эту проблему, я имею в виду получение уведомления день и время, которое я дал.
Также я хотел узнать, как изменить заголовок уведомления Chanel на мой текст для редактирования текста.
SetAlarmActivity
btn_ajouter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ajouterRDV();
LocalDate datebefore = (LocalDate) dateRDV.minusDays(1);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(datebefore.getYear(), datebefore.getMonthValue(),
datebefore.getDayOfMonth(), 9, 35, 0);
System.out.println(datebefore.getDayOfMonth());
System.out.println(datebefore.getYear());
creerAlarm(calendar);
}
});
private void creerAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("titreRDV", titre.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
AlarmReceiver
@Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "sa"; // here I should get the content of my editText from the Activity
String description = "sa";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.ic_note_black_24dp)
.setContentTitle("hi")
.setContentText("hi")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1,builder.build());
System.out.println("notification créer");
}
}