0

I want to make a notification to appear on a specific date at a specific time, as I wrote in the title. Now my alerts come in 10 seconds after pressing the button. If I need to set a specific date, do I need to do that as well? I need convert the date into milliseconds and give this parameter? Here's my code:

package com.example.timeman;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class ReminderBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notify")
            .setSmallIcon(R.drawable.ic_baseline_close_24)
            .setContentTitle("Salamabuliy")
            .setContentText("TextNotif")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            ;
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    notificationManager.notify(200, builder.build());
}

}

And OnClick Method:

public void onClick(View view) {
            Intent intent = new Intent(getActivity(), ReminderBroadcast.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

            long timeAtButtonClick = System.currentTimeMillis();

            long tenSecondsInMillis = 1000*10;

            alarmManager.set(AlarmManager.RTC, timeAtButtonClick + tenSecondsInMillis, pendingIntent);
            //displayNotification();
            OpenThis(view);
        }

2 Answers2

4

One option would be to find the duration in millis between now and the date you want it to appear on. For example;

LocalDateTime now = LocalDateTime.now();

LocalDateTime end = now.plusSeconds(10); // end date can be specific, this is an example

Duration difference = Duration.between(now, end);

long differenceMillis = difference.toMillis();
Jason
  • 5,154
  • 2
  • 12
  • 22
  • `System.out.println(differenceMillis);` will always give you `10000`. If this is what you intended to have, it's better to use `final long differenceMillis = 10000L;` – Arvind Kumar Avinash Aug 19 '20 at 11:51
  • 1
    The end date is intended to be dynamic, the example I provided was not specifically because it's an example. The user would provide the end date themselves using whatever mechanism they have to do so. – Jason Aug 19 '20 at 12:02
  • Jason - I've always liked your answers and especially your attitude. The only thing that I wanted to highlight in my comment is that out of the four lines in your answer, the last two are unnecessary. Since you are adding `10 seconds` to `now` to get `end`, there is no need to calculate the difference (which will always be 10 seconds). – Arvind Kumar Avinash Aug 19 '20 at 12:46
  • Thank you, I appreciate that. I didn't mean to come off as rude I just wanted to clarify, I could definitely be misunderstanding your point so I apologize. Let me try to explain again with a bit more context and a better example. If the user provided the `end` as a LocalDateTime that is in the future, for example `08/21/2020 00:01:00`, then we have no idea what the duration between now and `end` is, so we use a Duration. I only used `LocalDateTime#plusSeconds` as an example end date but that was likely a poor and confusing example. I hope this helped clarify. – Jason Aug 19 '20 at 13:02
  • 1
    Jason: `I didn't mean to come off as rude I just wanted to clarify` - I've mentioned a couple of times earlier that you have a heart of gold. No, your comment was not rude at all. It's all clear...wish you a good day! – Arvind Kumar Avinash Aug 19 '20 at 13:07
  • 2
    Awesome man, thank you! I hope you have a good day too man, I hope to see you around. I've definitely seen you around as well! I'm sure I've learned a thing or two from ya. – Jason Aug 19 '20 at 13:11
0

Use Instant#toEpochMilli() and Instant#plusSeconds as shown below:

import java.time.Instant;

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println(now.toEpochMilli());

        // After 10 seconds
        Instant after10SecFromNow = now.plusSeconds(10);
        long millisAfter10Sec = after10SecFromNow.toEpochMilli();
        System.out.println(millisAfter10Sec);

        // alarmManager.set(AlarmManager.RTC, millisAfter10Sec, pendingIntent);
    }
}

Output:

1597837345043
1597837355043

Learn more about java.time*, the modern Date-Time API, from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110