我想在特定日期前一天的固定时间在我的 Android 应用程序中推送通知。我已经在主要活动中创建了一个频道。
以下是第三个 Activity 中负责调度通知的方法的代码:
private void scheduleNotification(LocalDate date, String doctorName) {
try {
if (date != null) {
LocalDate notificationDate = date.minusDays(1);
Calendar calendar = Calendar.getInstance();
calendar.set(notificationDate.getYear(), notificationDate.getMonthValue() - 1, notificationDate.getDayOfMonth(), 2, 2, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AppointmentNotificationReceiver.class);
intent.putExtra("doctor_name", doctorName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getBaseContext(), "Notification scheduled for " + notificationDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")) + " at 13:00.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Error scheduling notification: " + e.getMessage() + ".", Toast.LENGTH_LONG).show();
}
}
这是通知接收器类的代码。
public class AppointmentNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String doctorName = intent.getStringExtra("doctor_name");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "ID12345678FFRREE")
.setContentTitle("Remainder")
.setContentText("Tomorrow.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details.
return;
}
notificationManager.notify(200, builder.build());
}
}
这是主活动中用于创建通道的代码部分:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("ID12345678FFRREE", "Channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("desc");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
一切正常,但根本不出现通知。相反,在通知的固定时间,如果我当前处于不同的活动,应用程序将返回到第一个活动(主活动)。
这样做就可以了:
public class AppointmentNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String doctorName = intent.getStringExtra("doctor_name");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "ID12345678FFRREE")
.setContentTitle("Remainder")
.setContentText("Tomorrow.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationChannel channel = new NotificationChannel("ID12345678FFRREE", "Channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("desc");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
notificationManager.notify(200, builder.build());
}
}