Ok, so this is a kind of a duplicate in a sense of a lot of questions for example this one: Using Service to run background and create notification from which I have taken code and got it to do what I need to work.
However there is a problem now with all the answers, they want to use WakefulBroadcastReceiver
which is now depreciated. I understand I now need to use JobService, so I tried updating the code in the previous post to look like this (using in part this tutorial https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html)
public class NotificationEventReceiver extends JobService {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
private static final int NOTIFICATIONS_INTERVAL = 1;
public static void setupAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.setRepeating(AlarmManager.RTC,
getTriggerAt(new Date()),
NOTIFICATIONS_INTERVAL * AlarmManager.INTERVAL_DAY,
alarmIntent);
}
private static long getTriggerAt(Date now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
//calendar.add(Calendar.HOUR, NOTIFICATIONS_INTERVAL_IN_HOURS);
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
return calendar.getTimeInMillis();
}
@Override
public boolean onStartJob(JobParameters params) {
Intent service = new Intent(getApplicationContext(), NotificationIntentService.class);
getApplicationContext().startService(service);
setupAlarm(getApplicationContext()); // reschedule the job
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
private static PendingIntent getStartPendingIntent(Context context) {
Intent intent = new Intent(context, NotificationEventReceiver.class);
intent.setAction(ACTION_START_NOTIFICATION_SERVICE);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static PendingIntent getDeleteIntent(Context context) {
Intent intent = new Intent(context, NotificationEventReceiver.class);
intent.setAction(ACTION_DELETE_NOTIFICATION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
And then in the Android Manifest
<service
android:name=".NotificationEventReceiver"
android:label="Notification Service"
android:permission="android.permission.BIND_JOB_SERVICE" />
When run I no longer get any notifications, so I clearly did something wrong, but I am at a loss as it what I need to do.