Tried to implement a notification that shows a notification every week, but it does not work.

alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 1); long timeToStart = calendar.getTimeInMillis(); if(System.currentTimeMillis() < timeToStart){ timeToStart += 7 * 24 * 60 * 60 * 1000; // one day } alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeToStart, AlarmManager.INTERVAL_DAY, alarmIntent); 

In general, I want to implement a notification, for example, every day at 12 noon, help to do it.

  • Read about JobScheduler - Sviat Volkov
  • @SviatVolkov is not exactly what JobScheduler for Android 5.0 (API 21) and higher needs, and I need what worked on API 16 - fcbarcafc

2 answers 2

Here is the code to start the AlarmManager , every day at 12 o'clock , which in turn will launch your receiver.

 Calendar notifyTime = DateHelper.getTodayCalendarWithoutTime(); notifyTime.set(Calendar.HOUR_OF_DAY, 12); notifyTime.set(Calendar.MINUTE, 0); notifyTime.set(Calendar.SECOND, 0); Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notifyTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

Inside the receiver, check the launch conditions for the notification (should it be run at all) and launch the required notification or several notifications, depending on the situation.

 public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (reminder != null) { // Запускаем уведомление MyNotification.notify(context, message, number); } } } 

The notifications themselves are best kept separately and I recommend in Android Studio to add them through the right mouse button - UI Component / Notification . There are very correct templates for different types of notifications with detailed comments.

  • Sorry, is there more similar information? - Artsait

API versions 26 and higher require installation of chanel. I propose such a way out of the situation:

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.pict01) .setContentTitle(Title) .setContentText(MainText) //.setLargeIcon(picture); .setContentIntent(resultPendingIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { CharSequence channelName = "c_name"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel notificationChannel = new NotificationChannel("3260", channelName, importance); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); notificationManager.createNotificationChannel(notificationChannel); builder.setChannelId(notificationChannel.getId()); } Notification notification = builder.build(); notificationManager.notify(a, notification);