I need to show repeating notifications to the user with different content. I use BroadcastReceiver and set notifications via AlarmManager.
I know that I can use AlarmManager.setRepeating
, but it shows the same content every time.
For example, I need to show notifications one time a week depending on the selected week by the user. And in every notification, I need to show current Week and some different text. I didn't find a solution on StackOverflow.
Here is the relevant code
long timeInMills = mSharedPreferences.getLong("key_millis", 0);
Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.setTimeInMillis(timeInMills);
alarmCalendar.set(Calendar.HOUR_OF_DAY, 10);
alarmCalendar.set(Calendar.MINUTE, 0);
int currentWeek = (int) Utils.getTotalWeeks(timeInMills);
if (currentWeek <= 20) {
AlarmHelper alarmHelper = AlarmHelper.getInstance();
for (int i = currentWeek + 1; i < 21; i++) {
alarmCalendar.add(Calendar.DATE, 7 * (currentWeek + 1));
long time = alarmCalendar.getTimeInMillis();
alarmHelper.setWeekAlarm(getApplicationContext(), WeekReceiver.class, time, i, i);
}
}
And the AlarmHelper
class is the following.
public class AlarmHelper {
private static AlarmHelper instance;
public static AlarmHelper getInstance() {
if (instance == null) {
instance = new AlarmHelper();
}
return instance;
}
public void setWeekAlarm(Context context, Class<?> receiver, long time, int week, int req_code) {
Intent intent = new Intent(context, receiver);
intent.putExtra("week_title", "your week " + week);
intent.putExtra("req_code", req_code);
//cancel already existed alarm
cancelWeekAlarm(context, receiver, req_code);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, req_code, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
public void cancelWeekAlarm(Context context, Class<?> receiver, int req_code) {
Intent intent = new Intent(context, receiver);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, req_code, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
The problem is that if the user is on the first week, so 20 notifications will be created and I think my approach is not very good and maybe you can help me find another solution?
I am not quite sure what is the purpose of your code, as I did not understand the question clearly. However, I think I found a problem in your code which I think I should share.
While you are adding a Calendar
event, I think you are trying to add weeks incrementally in different weeks. However, in your code looks like you are setting all the events in a certain week.
I am referring to the following code. Please see the comments in the code.
if (currentWeek <= 20) {
AlarmHelper alarmHelper = AlarmHelper.getInstance();
for (int i = currentWeek + 1; i < 21; i++) {
// I think the following line should contain currentWeek + i instead of currentWeek + 1
// alarmCalendar.add(Calendar.DATE, 7 * (currentWeek + 1));
alarmCalendar.add(Calendar.DATE, 7 * (currentWeek + i)); // Like this to create events in consecutive weeks.
long time = alarmCalendar.getTimeInMillis();
alarmHelper.setWeekAlarm(getApplicationContext(), WeekReceiver.class, time, i, i);
}
}
Let me know if that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.