Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show list of strings in notification in a given time slot?

I want to show the notifications in the specified time. Like I have a start time from when I want to see the notifications and the end time till when I want to see the notifications i.e the list of strings which should be displayed in a given time slot.

Also the list can be of any number specified by the user.

How can I decide the time of showing the notifications dynamically? Or how can I divide the time slot and strings uniformly?

For more clarifications here is the screen which shows the start time, end time and the count of strings to shown in notifications:

enter image description here

Please help. Thank you...

EDIT :

I am trying the given solution.

 List<String> times = new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                    howMany;
            for (int i = 0; i < howMany; i++) {

                Calendar calobj = Calendar.getInstance();
                calobj.setTime(start);
                calobj.add(Calendar.MINUTE, (int) (i * minutes));
                String time = dateFormat.format(calobj.getTime());
                times.add(time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("timesList", times.toString());
        return times;
    }

    public static void showNotification(
            List<String> timeList, Context context,
            String quote
    ) {

        Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                notifyIntent, PendingIntent.FLAG_ONE_SHOT
        );

        notifyIntent.putExtra("title", context.getString(R.string.app_name));

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

        for (String time : timeList) {
            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );
            Date date;
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
            try {
              date = dateFormat.parse(time);
                System.out.println(date);

            alarmManager
                    .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            date.getTime(),
                            date.getTime(),
                            pendingIntent
                    );

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }

In my Receiver building the notification.

  public class MyNewIntentReceiver extends BroadcastReceiver {

    public MyNewIntentReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager powerManager = (PowerManager) context.getSystemService(
                Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock =
                powerManager.newWakeLock(
                        PowerManager.PARTIAL_WAKE_LOCK,
                        "dailyfaith:wakelog"
                );
        wakeLock.acquire();

        // get id, titleText and bigText from intent
        int NOTIFY_ID = intent.getIntExtra("notify_id", 0);
        String titleText = intent.getStringExtra("title");
        String bigText = intent.getStringExtra("quote");

        // Create intent.
        Intent notificationIntent = new Intent(context, MainActivity.class);

        // use NOTIFY_ID as requestCode
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT
        );

        // get res.
        Resources res = context.getResources();

        // build notification.
        Notification.Builder builder = new Notification.Builder(context)
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_daily_faith_icon)
                .setAutoCancel(true)
                .setContentTitle(titleText)
                .setSound(RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentText(bigText);

        Log.d("notificationBuild", "Notification Builder set");

    /*    // check vibration.
        if (mPrefs.getBoolean("vibration", true)) {
            builder.setVibrate(new long[]{0, 50});
        }*/

   /*     // create default title if empty.
        if (titleText.equals("")) {
            builder.setContentTitle(
                    context.getString(R.string.app_name));
        }*/

        // show notification. check for delay.
        builder.setWhen(System.currentTimeMillis());
        Log.d("notificationSetWhen", "Notification set when triggered");

        Notification notification = new Notification.BigTextStyle(builder)
                .bigText(bigText).build();

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFY_ID, notification);

        wakeLock.release();
    }
}

From Activity :

  @Override
    public void onTimeSet(
            TimePickerDialog view, int hourOfDay, int minute, int second
    ) {
        String hourString = hourOfDay < 10 ? "0" + hourOfDay : "" + hourOfDay;
        String minuteString = minute < 10 ? "0" + minute : ":" + minute;
        String time = hourString + minuteString;

        if (startTimeSelected) {
            startTime = time;
            textViewStartTime.setText(time);
        }
        else if (endTimeSelected) {
            endTime = time;
            textViewEndTime.setText(time);
        }

        String count = (String) textViewQuoteCount.getText();
        count.replace("X","");

        if(startTimeSelected && endTimeSelected)
        {
            Utils.setAlarmTimeList(startTime, endTime, Integer.parseInt(count));
            Utils.showNotification(timeList); // not sure how to send the list of strings - quotes
        }

        tpd = null;
    }

I am passing the time array to pending intent, but notification is not getting triggered. I thought for alarm I also need to give the current date so I again formatted time for each notification.

But that also did not work. Any suggestions?

EDIT :

I have updated the answer by Erwin. I am getting the date also with the time now but still as I debug the receiver is also not getting called.

I have set the receiver in the manifest file :

<receiver
    android:name = ".MyNewIntentReceiver"
    android:enabled = "true"
    android:exported = "false" />

Log of timesList

  D/timesList: [Tue May 19 16:21:00 GMT+05:30 2020, Tue May 19 16:24:00 GMT+05:30 2020, Tue May 19 16:27:00 GMT+05:30 2020, Tue May 19 16:30:00 GMT+05:30 2020, Tue May 19 16:33:00 GMT+05:30 2020, Tue May 19 16:36:00 GMT+05:30 2020, Tue May 19 16:39:00 GMT+05:30 2020, Tue May 19 16:42:00 GMT+05:30 2020, Tue May 19 16:45:00 GMT+05:30 2020, Tue May 19 16:48:00 GMT+05:30 2020]

What can be the issue:

I tried to give current time to the pending intent as :

 alarmManager
                .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        System.currentTimeMillis(),
                        System.currentTimeMillis(),
                        pendingIntent
                );

Then I got the notification. But not getting when I am setting a date into that.

EDIT 2

   public static List<Date> setAlarmTimeList(String startTime, String endTime, int howMany) {
        List<Date> times = new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                    (howMany - 1);
            Calendar calobj;
            for (int i = 0; i < howMany; i++) {

                calobj = Calendar.getInstance();
                calobj.set(Calendar.HOUR_OF_DAY, Integer.valueOf(dateFormat.format(start).split(":")[0]));
                calobj.set(Calendar.MINUTE, Integer.valueOf(dateFormat.format(start).split(":")[1]));
                calobj.add(Calendar.MINUTE, (int) (i * minutes));
                calobj.set(Calendar.SECOND, 0);
                times.add(calobj.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("timesList", times.toString());
        return times;
    }


    public static void showNotification(
            List<Date> timeList, Context context,
            String quote
    ) {

        for (Date date : timeList) {
            Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

            notifyIntent.putExtra("title", context.getString(R.string.app_name));

            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );
            int randomInt = new Random().nextInt(1000);

            notifyIntent.putExtra("requestCode",randomInt);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    randomInt,
                    notifyIntent, PendingIntent.FLAG_ONE_SHOT

            );

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


            Log.d("date",String.valueOf(date.getTime()));

         /*   long afterTwoMinutes = SystemClock.elapsedRealtime() + 60 * 1000;*/
            long afterTwoMinutes = System.currentTimeMillis();

            Log.d("aftertwoMinutes",String.valueOf(afterTwoMinutes));

            long datetimer = date.getTime();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                alarmManager.setExactAndAllowWhileIdle
                        (AlarmManager.ELAPSED_REALTIME_WAKEUP,
                               date.getTime(), pendingIntent);
            else
                alarmManager.setExact
                        (AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                date.getTime(), pendingIntent);
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }


public class MyNewIntentReceiver extends BroadcastReceiver {

    public MyNewIntentReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {

        int NOTIFY_ID = intent.getIntExtra("notify_id", 0);
        String titleText = intent.getStringExtra("title");
        String bigText = intent.getStringExtra("quote");
        int requestCode = intent.getIntExtra("requestCode",0);
        sendNotification(context,bigText,NOTIFY_ID,requestCode);
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library

    }

    public static void sendNotification(Context mcontext, String messageBody,
            int notify_id,int requestCode) {
        Intent intent = new Intent(mcontext, HomeScreenActivity.class);
        PendingIntent pendingIntent = PendingIntent
                .getActivity(mcontext, requestCode /* Request code */, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        NotificationManager notificationManager = (NotificationManager) mcontext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Uri defaultSoundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel
                    (
                            mcontext.getString(R.string.default_notification_channel_id),
                            "Rewards Notifications",
                            NotificationManager.IMPORTANCE_HIGH
                    );

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat
                .Builder(mcontext, mcontext.getString(R.string.default_notification_channel_id))
                .setContentTitle(mcontext.getString(R.string.app_name))
                .setSmallIcon(R.drawable.ic_daily_faith_icon)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


        notificationManager.notify(notify_id /* ID of notification */,
                notificationBuilder.build());
    }
}

Not working for date.getTime(), System.currentTimeInMilliseconds() working for SystemClock

like image 881
Sid Avatar asked Oct 16 '22 03:10

Sid


1 Answers

try this,

    public static List<Date> setAlarmTimeList(String startTime, String endTime, int howMany) {
    List<Date> times = new ArrayList<>();
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        Date start = dateFormat.parse(startTime);
        Date end = dateFormat.parse(endTime);
        long minutes = ((end.getTime() - start.getTime()) / 1000 / 60) /
                (howMany - 1);
        Calendar calobj;
        for (int i = 0; i < howMany; i++) {

            calobj = Calendar.getInstance();
            calobj.set(Calendar.HOUR_OF_DAY, Integer.valueOf(dateFormat.format(start).split(":")[0]));
            calobj.set(Calendar.MINUTE, Integer.valueOf(dateFormat.format(start).split(":")[1]));
            calobj.add(Calendar.MINUTE, (int) (i * minutes));
            calobj.set(Calendar.SECOND, 0);
            times.add(calobj.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Log.d("timesList", times.toString());
    return times;
}

get start time and end time in millis and then divide it to how many times.

for alarm manager,

public static void showNotification(List<Date> timeList, Context context, String quote) {

        Intent notifyIntent = new Intent(context, MyNewIntentReceiver.class);

        notifyIntent.putExtra("title", context.getString(R.string.app_name));

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

        for (Date time : timeList) {
            final int random = new Random().nextInt();
            notifyIntent.putExtra("notify_id", random);

            notifyIntent.putExtra(
                    "quote",
                    quote
            );

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, random,
                    notifyIntent, PendingIntent.FLAG_ONE_SHOT
            );

            alarmManager
                    .setInexactRepeating(AlarmManager.RTC_WAKEUP,
                            time.getTime(),
                            AlarmManager.INTERVAL_DAY,
                            pendingIntent
                    );
        }

        Log.d("notificationIntentSet", "Utils, pending intent set");
    }
like image 89
Erwin Avatar answered Oct 20 '22 23:10

Erwin