Я хочу отправить уведомление в 2:00 PM. Каждый день я пробовал AlarmManager
, но здесь не работает мой код
Home. java
Intent intentAlarm = new Intent(Home.this, NotificationReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(Home.this, 1, intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
NotificationReciever. java
private String[] notification;
@Override
public void onReceive(Context context, Intent intent) {
notification = context.getResources().getStringArray(R.array.notification);
int randomIndex = new Random().nextInt(notification.length);
String random = notification[randomIndex];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("NotificationChannel", "blabla", importance);
channel.setDescription("blabla");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Intent intent2 = new Intent(context, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
"NotificationChannel")
.setSmallIcon(R.mipmap.app_icon)
.setContentTitle("blabla")
.setContentText(randomVerse)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(1, builder.build());
}