android - 设置提醒后一分钟内弹出每周通知
问题描述
我是 android 新手,目前正在开发通知。
我有两种类型的通知要显示。一种是每周提醒,另一种是每月提醒。
以下是我设置每周提醒的方式:
manager.setRepeating(AlarmManager.RTC_WAKEUP, currentTime, AlarmManager.INTERVAL_DAY *7, pendingIntent);
以下是我设置每月提醒的方式:
long monthlyDuration = getDuration();
manager.setRepeating(AlarmManager.RTC_WAKEUP, currentTime, monthlyDuration, pendingIntent);
private long getDuration(){
// get todays date
Calendar cal = Calendar.getInstance();
// get current month
int currentMonth = cal.get(Calendar.MONTH);
// move month ahead
currentMonth++;
// check if has not exceeded threshold of december
if(currentMonth > Calendar.DECEMBER){
// alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
currentMonth = Calendar.JANUARY;
// Move year ahead as well
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
}
// reset calendar to next month
cal.set(Calendar.MONTH, currentMonth);
// get the maximum possible days in this month
int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, maximumDay);
long thenTime = cal.getTimeInMillis();
return (thenTime);
}
这是通知代码:
int importance = NotificationManager.IMPORTANCE_HIGH;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "default");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.col_week_rem_description);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
builder.setAutoCancel(false);
builder.setChannelId(CHANNEL_ID);
builder.setContentTitle(context.getResources().getString(R.string.app_name));
builder.setContentText(context.getResources().getString(R.string.notification_text));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.ic_launcher_notification);
int myColor =
context.getResources().getColor(R.color.notification_icon_bg);
builder.setColor(myColor);
}
else {
builder.setSmallIcon(R.drawable.ic_launcher);
}
builder.setContentIntent(pendingIntent);
// builder.setOngoing(true);
// builder.setNumber(100);
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
我面临的问题是,通知会在设置显示提醒选项后的一分钟或两分钟内弹出。我想做的是当用户选择每周提醒时,他/她应该在7天后同时收到通知。即,如果我选择今天凌晨 3 点的每周提醒。然后我应该在用户选择每周提醒之日起 7 天后的 3 点收到通知。每月提醒也以同样的方式工作。
我在这里和其他地方经历了很多线程,但无法解决问题。谁能帮我解决这个问题。
谢谢你。
解决方案
public static void setReminder(Context ctx) {
Calendar setcalendar = Calendar.getInstance();
// setcalendar.set(Calendar.DAY_OF_WEEK, 3);
setcalendar.set(Calendar.HOUR_OF_DAY, //notification hr);
setcalendar.set(Calendar.MINUTE, //notification min.);
setcalendar.set(Calendar.SECOND, //notification sec.);
setcalendar.set(Calendar.DAY_OF_WEEK, //notification day);
Intent intent = new Intent(ctx, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
long timeMs = setcalendar.getTimeInMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
}
Log.e("", "setReminder: reminder has been set");
}
推荐阅读
- java - BiConsumer/BiFunction/BiPredicate 中的“Bi”是什么意思?
- vue.js - Retrieve data's component from custom directive
- python - 第三行输入未读
- rsqlite - RSQLite 不创建本地数据库
- ios - 如何在不使用情节提要的情况下在两个视图之间添加 UIButton?
- c# - Service Fabric 无状态服务能否通知其他实例?
- javascript - 如何让 setInterval() 在每次迭代之前等待一个间隔(比如 10 秒)?
- php - Laravel 更新 WhereHas 中的关系列
- php - 如何在我键入时通过 Jquery Ajax 将输入文本中的值传递给 php 函数?WordPress
- javascript - 如何使用 javascript 渲染 jinja 或 jinja2 模板