Sid
Sid

Reputation: 2862

How to set repeated notification for specific day of week?

I am looking for to set a notification on week days. I have added a string of days in alert dialog to show the days.

I want to set the day in calendar which user has selected in the alert dialog. Also, this should repeat next week on same day.

I have put time picker dialog to choose time of notification. But if I do c.getTime() , so I get the current date and time.

How to set notification for other days? Like if today is monday and I want to create notification on wed? How can I set wed in calendar?

  c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);

        notification = c.getTime();
        notificationTime = df.format(notification);
        notifyTime.setText(notificationTime);
        Toast.makeText(getApplicationContext(),String.valueOf(notification),Toast.LENGTH_SHORT).show();

For notification I am using alarm manager.

 private void setAlarm(Calendar targetmCalen) {

    Intent intent = new Intent(getBaseContext(),NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,targetmCalen.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY *7, pendingIntent);

    ComponentName receiver = new ComponentName(getApplicationContext(),NotificationReceiver.class);
    PackageManager pm = getApplicationContext().getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}

Alert dialog for selecting day:

  selectDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);

            builder.setSingleChoiceItems(R.array.day_array, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {

                            String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);

                            selectDay.setText(day[item]);

                            dialog.dismiss();
                        }
                    });
            builder.show();
        }
    });

Do I have to set dayofweek in calendar instance inside alert dialog? Like

c.set(Calendar.DAY_OF_WEEK)?

EDIT :

I tried to do this way. But dose this is may be going one day ahead every time if i choose day multiple times.

 selectDay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);

            builder.setSingleChoiceItems(R.array.day_array, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {

                            String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);

                            selectDay.setText(day[item]);

                            dayOfWeek = day[item];

                            switch (dayOfWeek)
                            {
                                case  "Mon":
                                    c.set(Calendar.DAY_OF_WEEK,2);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;
                                case "Tue":

                                    c.set(Calendar.DAY_OF_WEEK, 3);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;
                                case "Wed":
                                    c.set(Calendar.DAY_OF_WEEK, 4);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Thu":
                                    c.set(Calendar.DAY_OF_WEEK, 5);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Fri":
                                    c.set(Calendar.DAY_OF_WEEK, 6);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Sat":
                                    c.set(Calendar.DAY_OF_WEEK, 7);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                                    break;

                                case "Sun":

                                    c.set(Calendar.DAY_OF_WEEK,1);
                                    c.getTime();
                                    Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
                            }

                            dialog.dismiss();
                        }
                    });
            builder.show();
        }
    });

Please help..

Upvotes: 1

Views: 3183

Answers (2)

exilour
exilour

Reputation: 556

I hope you already have solved your problem. But I will post my solution nonetheless. So if anyone stumps upon.

private void sendNotification(){
    Intent notificationIntent = new Intent(HomeActivity.this, NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            NOTIFICATION_REQUEST_CODE,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, CONST.NOTIFICATION_TIME_HOUR);
    calendar.set(Calendar.MINUTE, 0);
    for (int i = 1; i < 8; ++i){
        if (i != 6){
            calendar.set(Calendar.DAY_OF_WEEK, i);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CONST.NOTIFICATION_INTERVAL, pendingIntent);
        }
    }
}

Here long NOTIFICATION_INTERVAL = 7 * 24 * 60 * 60 * 1000; & int NOTIFICATION_TIME_HOUR = 9;

Upvotes: 0

Pratik Tank
Pratik Tank

Reputation: 2252

public class TimerReceiver extends BroadcastReceiver {

public  void scheduleAlarms(Context paramContext) {

    Calendar calendar = Calendar.getInstance();

    if (strmyTime.contains("One Week")) {
        calendar.set(Calendar.DATE, 7);

        registerReceiver(paramContext, calendar, AlarmManager.INTERVAL_DAY * 7);
    }

}

public  void registerReceiver(Context paramContext, Calendar calender, long interval) {

    AlarmManager localAlarmManager = (AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE);
    PendingIntent localPendingIntent = PendingIntent.getService(paramContext, 0,
            new Intent(paramContext, NotificationService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    localAlarmManager.setRepeating(AlarmManager.RTC, calender.getTimeInMillis(), interval, localPendingIntent);

}

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


    scheduleAlarms(context);
    context.startService(new Intent(context, Notification.class));
        }

}

Upvotes: 0

Related Questions