简体   繁体   English

如何用不同的内容进行重复通知

[英]How to make repeating notifications with different content

I need to show repeating notifications to the user with different content. 我需要向具有不同内容的用户显示重复通知。 I use BroadcastReceiver and set notifications via AlarmManager. 我使用BroadcastReceiver并通过AlarmManager设置通知。

I know that I can use AlarmManager.setRepeating , but it shows the same content every time. 我知道我可以使用AlarmManager.setRepeating ,但是每次都显示相同的内容。

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. 在每个通知中,我需要显示当前的Week和一些不同的文本。 I didn't find a solution on StackOverflow. 我在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. AlarmHelper类如下。

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? 问题是,如果用户在第一周,那么将创建20条通知,我认为我的方法不是很好,也许您可​​以帮助我找到另一个解决方案?

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. 当您添加Calendar事件时,我认为您正在尝试在不同的星期中逐渐增加星期。 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! 让我知道是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM