когда я использую AlarmManager.SetRepeating () для отправки уведомлений два раза в день, он отлично работает для api <23 в api23 и выше, он не отправляет никаких уведомлений, поэтому я использую setExactAndAllowWhileIdle () для Build.VERSION.SDK_INT> = 23он работал только первый день и после этого
public void OnRepeatingNotification() {
String ACTION_ONE = "android.intent.action.ACTION_ONE";
String ACTION_TWO = "android.intent.action.ACTION_TWO";
String title = getResources().getString(R.string.app_name);
String content1 = getResources().getString(R.string.alarmone);
String content2 = getResources().getString(R.string.alarmtwo);
boolean sound=settings.getBoolean("sett_1",false);
boolean vibrate=settings.getBoolean("sett_2",false);
String[] parts = getTimes();
String sunriseTime = parts[0].substring(0, 5) + " " + parts[0].substring(5);
String[] sunriseTimeArray = sunriseTime.split(" ");
String[] sunriseDivision = sunriseTimeArray[0].split(":");
String sunsetTime = parts[1].substring(0, 5) + " " + parts[1].substring(5);
String[] sunsetTimeArray = sunsetTime.split(" ");
String[] sunsetDivision = sunsetTimeArray[0].split(":");
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Intent myIntent = new Intent(this, NotificationReciever.class);
myIntent.setAction(ACTION_ONE);
myIntent.putExtra("title", title);
myIntent.putExtra("content1", content1);
myIntent.putExtra("sound",sound);
myIntent.putExtra("vibrate",vibrate);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1253, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
cal.set(Calendar.HOUR_OF_DAY, h1);
cal.set(Calendar.MINUTE, Integer.parseInt(sunriseDivision[1]));
cal.set(Calendar.SECOND, 0);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
//AlarmManager.INTERVAL_DAY, pendingIntent);
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),
pendingIntent);
Intent myIntent2 = new Intent(this, NotificationReciever.class);
myIntent2.setAction(ACTION_TWO);
myIntent2.putExtra("title", title);
myIntent2.putExtra("content2", content2);
myIntent2.putExtra("sound",sound);
myIntent2.putExtra("vibrate",vibrate);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1263, myIntent2,
PendingIntent.FLAG_CANCEL_CURRENT);
// Set the time for second alarm here
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, h2);
cal.set(Calendar.MINUTE, Integer.parseInt(sunsetDivision[1]));
cal.set(Calendar.SECOND, 0);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
//AlarmManager.INTERVAL_DAY, pendingIntent2);
AlarmManagerCompat.setExactAndAllowWhileIdle( alarmManager, AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent2 );
}
код широковещательного получателя для отправки уведомлений
@Override
public void onReceive(final Context context, Intent intent) {
String title = intent.getStringExtra("title");
String content1 = intent.getStringExtra("content1");
String content2 = intent.getStringExtra("content2");
boolean vibrate = intent.getBooleanExtra("vibrate", false);
boolean sound = intent.getBooleanExtra("sound", false);
NotificationID n=new NotificationID();
int NOTIFICATION_ID = n.getID();
Intent in = new Intent(context, DailyAzkaar.class);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//If on Oreo then notification required a notification channel.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default");
builder.setSmallIcon(R.mipmap.ic_launcher);
if (vibrate == true)
builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
if (sound == true) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
}
builder.setContentTitle(context.getString(R.string.app_name));
if (intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_ONE")) {
in.putExtra("activity", 1);
in.putExtra("poss", 0);// new
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1253, in, PendingIntent.FLAG_ONE_SHOT);
builder.setContentText(content1);
builder.setContentTitle(title);
builder.setContentIntent(pendingIntent).setAutoCancel(true);
} else {
in.putExtra("activity", 2);
in.putExtra("poss", 0);// new
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1263, in, PendingIntent.FLAG_ONE_SHOT);
builder.setContentText(content2);
builder.setContentTitle(title);
builder.setContentIntent(pendingIntent).setAutoCancel(true);
}
Notification notification = builder.build();
//int notificationID = 0;
notificationManager.notify(NOTIFICATION_ID, notification);
}