Я получаю текущее время в миллисекундах и добавляю к нему еще 60000(1 минута), но уведомление приходит не через минуту, а сразу.
Код:
public class MainActivity extends AppCompatActivity {
private static final int NOTIFY_ID = 101;
private static String CHANNEL_ID = "test channel";
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChanel();
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textForNotification = editText.getText().toString();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle("Напоминание")
.setContentText(textForNotification)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setWhen(System.currentTimeMillis() + 60000);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
notificationManager.notify(NOTIFY_ID, builder.build());
}
});
}
private void createNotificationChanel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Some description");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}