One of the functionality on my app is to send a daily notifications at certain hour. It works properly but I woudl like to send these notifications only for a week and finish it after that.
I want to make it like that : user click the button and it activate sending notifications everyday for a week. Is there a posibility to count days or maybe how many times function was performed ? I have no clue how to make it..
This is my code :
Creating channel :
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
private void createNotificationChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel( CHANNEL_1_ID, "channel 1", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
}
Notification :
public class WeekChallengeBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyDBHandler myDBHandler = new MyDBHandler(context, null,null,3);
ChallengesManager challengesManager = new ChallengesManager(context);
int id;
id = challengesManager.GetID();
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_1_ID )
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle( myDBHandler.loadChallengeNotificationRow1(id))
.setContentText(myDBHandler.loadChallengeNotificationRow2(id))
.setStyle(new NotificationCompat.BigTextStyle())
.setPriority(Notification.PRIORITY_MAX)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(false)
.setVibrate(new long[]{1000});
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(2, notification.build());
}
}
Function that activate notification :
void ActivateNotifications(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
if (calendar.getTime().compareTo((new Date())) < 0)
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
Intent intent = new Intent(mContext.getApplicationContext(), WeekChallengeBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if(alarmManager != null){
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
}
}