I wrote this program to sync data with the server. Before checking it with the server. I wrote a program to send notifications every 15 minutes. My phone is oppo A71 Android version 7.1
The following code is not working when I closed the app.
MainActivity.java
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(
MyPeriodicWork.class,15, TimeUnit.MINUTES)
.addTag("send data") .build();
WorkManager.getInstance().enqueue(periodicWorkRequest);
MyPeriodicWork.java
public class MyPeriodicWork extends Worker {
private static final String FILE_NAME = "chata.txt";
private static final String TAG = "MyPeriodicWork";
public MyPeriodicWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
showNotif();
Log.e(TAG,"doWork:work is done");
return Result.success();
}
public void showNotif(){
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,intent,0);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String strDate = "Current Time : " + mdformat.format(calendar.getTime());
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(getApplicationContext(),"14")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Event Handler")
.setContentText("Helloo"+strDate)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(4,notificationCompat.build());
}
}
Do I need to add some permissions to manifest file. If yes what are those codes.