change schedule notification methode

This commit is contained in:
jacques 2020-08-21 10:34:12 +02:00
parent c4175e06e3
commit 2ef64e31e3
4 changed files with 55 additions and 22 deletions

View file

@ -1,8 +1,6 @@
package net.foucry.pilldroid;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
@ -10,7 +8,6 @@ import android.content.Context;
import android.content.Intent;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@ -34,12 +31,10 @@ import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static net.foucry.pilldroid.UtilDate.date2String;
import static net.foucry.pilldroid.Utils.intRandomExclusive;
@ -63,6 +58,7 @@ public class MedicamentListActivity extends AppCompatActivity {
final Boolean DEMO = false;
final Boolean DBDEMO = false;
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
public String CHANNEL_ID = null;
@Override
public void onStart() {
@ -99,7 +95,6 @@ public class MedicamentListActivity extends AppCompatActivity {
public void constructMedsList()
{
Medicament currentMedicament;
dbHelper = new DBHelper(getApplicationContext());
if (!(medicaments == null)) {
@ -442,7 +437,7 @@ public class MedicamentListActivity extends AppCompatActivity {
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, ("Job scheduled " + cal(now.getTime() + 60000)));
Log.d(TAG, ("Job scheduled " + (now.getTime() + 60000)));
} else {
Log.d(TAG, "Job scheduling failed");
}

View file

@ -38,8 +38,6 @@ public class NotificationPublisher extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Receive notification");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
String message = intent.getStringExtra(KEY_MESSAGE);
String title = intent.getStringExtra(KEY_TITLE);
@ -63,6 +61,10 @@ public class NotificationPublisher extends BroadcastReceiver {
builder.setSound(alarmSound);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, builder.build());
notificationManager.notify(notificationId, builder.build());
}

View file

@ -1,15 +1,23 @@
package net.foucry.pilldroid;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.icu.util.Calendar;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.Date;
import java.util.List;
@ -24,7 +32,7 @@ public class PillDroidJobService extends JobService {
private static final String TAG = JobService.class.getName();
private boolean jobCancelled = false;
private List<Medicament> medicaments = null;
private String CHANNEL_ID = null;
private DBHelper dbHelper = new DBHelper(this);
@Override
@ -35,12 +43,19 @@ public class PillDroidJobService extends JobService {
return true;
}
/**
* Grab sorted list of medicaments
* test dateAlert of the first of the list
* if dateAlert < now
* schedule notification
* @param JobParameters params
*/
private void doBackgroundWork(final JobParameters params) {
if (jobCancelled) {
return;
}
medicaments = dbHelper.getAllDrugs();
List<Medicament> medicaments = dbHelper.getAllDrugs();
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
@ -64,6 +79,7 @@ public class PillDroidJobService extends JobService {
}
long delay = dateSchedule - now.getTime();
createNotificationChannel();
scheduleNotification(getApplicationContext(), delay);
}
@ -88,18 +104,36 @@ public class PillDroidJobService extends JobService {
private void scheduleNotification(Context context, long delay) {
Log.d(TAG, "scheduleNotification delay == " + delay);
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.putExtra(NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy));
notificationIntent.putExtra(NotificationPublisher.KEY_TITLE, getString(R.string.app_name));
notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_pill)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.pharmacy))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
int notificationId = 666;
notificationManager.notify(notificationId, builder.build());
}
/**
* createNotificationChannelid for android API >= 28
* @param Context context
* @return String channel_id
*/
private void createNotificationChannel() {
Log.d(TAG, "start create notification channel");
CharSequence name = getString(R.string.channel_name);
String description = 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 = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
}
}

View file

@ -25,4 +25,6 @@
<string name="enter_cip_13_here">Enter cip 13 here..</string>
<string name="enter_cip_13">Enter CIP 13</string>
<string name="pharmacy">You must go to the pharmacy</string>
<string name="channel_name">pilldroidChannel</string>
<string name="channel_description">PillDroid NotificationChannel</string>
</resources>