簡體   English   中英

如何用不同的內容進行重復通知

[英]How to make repeating notifications with different content

我需要向具有不同內容的用戶顯示重復通知。 我使用BroadcastReceiver並通過AlarmManager設置通知。

我知道我可以使用AlarmManager.setRepeating ,但是每次都顯示相同的內容。

例如,我需要每周顯示一次通知,具體取決於用戶選擇的一周。 在每個通知中,我需要顯示當前的Week和一些不同的文本。 我在StackOverflow上找不到解決方案。

這是相關的代碼

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);
    }
}

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();
    }
}

問題是,如果用戶在第一周,那么將創建20條通知,我認為我的方法不是很好,也許您可​​以幫助我找到另一個解決方案?

我不太清楚您的代碼的目的是什么,因為我不太清楚這個問題。 但是,我認為我在您的代碼中發現了一個問題,我應該與您分享。

當您添加Calendar事件時,我認為您正在嘗試在不同的星期中逐漸增加星期。 但是,在您的代碼中,您似乎正在設置一周中的所有事件。

我指的是以下代碼。 請查看代碼中的注釋。

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);
    }
}

讓我知道是否有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM