仅在特定日期和时间设置通知

时间:2019-07-01 15:43:24

标签: android alarmmanager android-notifications android-alarms

我已经使用AlarmManager创建了通知,但仅在创建警报时收到了通知。我该如何解决此问题,我的意思是在给出的日期和时间收到通知。 我也想知道如何将通知香奈儿标题更改为我的编辑文本内容。

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");
}

}

0 个答案:

没有答案