У меня недавно была эта проблема, и я решил ее с помощью приемника вещания.
Несмотря на то, что WakefulBroadcastReceiver устарел, его родительский класс не устарел.
Intent intent = new Intent(context, ClassReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Я использую диспетчер тревог setRepeating () и RTC_WAKEUP, см. Руководство по соответствующим типам тревог для соответствующего типа тревоги
также, см. Ссылку ниже, чтобы увидеть лучшие практики по тревогам. Рекомендации по оповещениям
Вы должны указать приемник вещания в манифесте
<receiver
android:name=".ClassReminderReceiver"
android:enabled="true"
android:exported="true" />
Это класс приемника вещания
public class ClassReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ClassReminderNotification.notify(context);
}
}
Вы сказали, что хотите создать уведомление, но, как упомянул @Chuong Le Van, я не вижу, где вы определяете уведомление в своем коде.
Ниже приведен класс ClassNotification
class ClassReminderNotification {
static void notify(Context context) {
createNotificationChannel(context);
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
"class_reminder")
.setSmallIcon(R.drawable.ic_school_black_24dp)
.setContentTitle("Your Notification Title")
.setContentText("Your Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
int notificationId = 1;
notificationManager.notify(notificationId, builder.build());
}
private static void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String CHANNEL_ID = "class_reminder";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name,
importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager =
context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
Вот руководство гугла Обзор уведомлений Обзор уведомлений
Здесь также находится официальное руководство по созданию уведомления Официальное руководство по созданию уведомления .
Надеюсь, это поможет.