Java Alarm Manager每天在特定时间(星期日除外)推送通知,并在触发时禁用推送显示
当用户第一次登录应用程序时,我使用Alarm Manager和Pending Intent,每天在特定截止时间前1小时启用推送通知 但是,我希望它每天都推,除了星期天。我怎么做?(请注意,我尝试了setrepeating,但它不适用于我拥有的新版本,即API 26) 而且,每次我登录时,它都会推送通知。如何防止它在登录时显示(第二天触发) 我在登录时的第一个CatalogeFragment中有一个名为scheduleNotification的方法,该方法包含以下内容:Java Alarm Manager每天在特定时间(星期日除外)推送通知,并在触发时禁用推送显示,java,android,alarmmanager,android-pendingintent,Java,Android,Alarmmanager,Android Pendingintent,当用户第一次登录应用程序时,我使用Alarm Manager和Pending Intent,每天在特定截止时间前1小时启用推送通知 但是,我希望它每天都推,除了星期天。我怎么做?(请注意,我尝试了setrepeating,但它不适用于我拥有的新版本,即API 26) 而且,每次我登录时,它都会推送通知。如何防止它在登录时显示(第二天触发) 我在登录时的第一个CatalogeFragment中有一个名为scheduleNotification的方法,该方法包含以下内容: Intent no
Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.putExtra("notif_content", content);
notificationIntent.putExtra("isEnglish", isEnglish);
notificationIntent.putExtra("notif_id", notificationId);
notificationIntent.putExtra("hour", "" + (Integer.parseInt(hour) - 1));
notificationIntent.putExtra("mins", mins);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
loginPreferences = getActivity().getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
if (loginPreferences.getBoolean("FirstTimeLogin", true)) {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
loginPrefsEditor.putBoolean("FirstTimeLogin", false);
loginPrefsEditor.apply();
}
在我的课堂上
public void onReceive(final Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String isEnglish = intent.getStringExtra("isEnglish");
final String id = "my_channel_id_01";
CharSequence name = "channel_name";
String description = "channel_description";
int importance = NotificationManager.IMPORTANCE_LOW;
int notificationId = intent.getIntExtra("notif_id", 0);
String hour = intent.getStringExtra("hour");
String mins = intent.getStringExtra("mins");
String cutoffHour = "" + (Integer.parseInt(hour) + 1);
String cutofftime = cutoffHour + ":" + mins;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
calendar.set(Calendar.MINUTE, Integer.parseInt(mins));
String content = "Please place order before " + cutofftime + " AM for today's delivery";
Intent notificationIntent = new Intent(context, AlarmReceiver.class);
notificationIntent.putExtra("notif_id", Integer.parseInt(new SimpleDateFormat("ddHHmmss").format(new Date())));
notificationIntent.putExtra("hour", hour);
notificationIntent.putExtra("mins", mins);
notificationIntent.putExtra("isEnglish", isEnglish);
notificationIntent.putExtra("notif_content", content);
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, id)
.setContentTitle("Gentle reminder")
.setContentText(content)
.setSmallIcon(R.drawable.logo)
.setAutoCancel(true);
Intent notifIntent = new Intent(context, LoginActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, notificationId, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(activity);
Notification notification = builder.build();
notificationManager.notify(notificationId, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 24 * 60 * 60 * 1000, pendingIntent);
如果您想在周日以外的每天触发警报,可以使用以下两种方法之一: -将警报设置为第二天触发。当警报响起时,检查第二天是否是星期天。如果是星期天,则将警报设置为在2天后触发(星期一),否则将警报设置为在第二天触发
-设置每天触发的重复警报。当警报响起时,检查是否是星期天,如果是,则忽略警报。您的问题尚不清楚。请准确解释正在发生或未发生的事情。每天推送通知。除了星期天,我希望它每天都推送。如何忽略警报?当调用
onReceive()
时,您可以通过不执行任何操作来忽略警报。