I’m really new to java, and I want to make a reminder app, and I am currently stuck on sending a notification to the user when user’s app is fully closed (Destroyed). Please help me, would be a much appreciate
My MainActivity
package com.example.notificationtest;
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.PackageManagerCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
Button button;
String medicineName = "Medicine";
String dayTime = "Mon, 13:27:00";
Date timeSet;
Date date1;
Boolean running = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
button = findViewById(R.id.notify);
// Checking the Version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Check if permission is enabled or not
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED) {
// If Permission is NOT Granted Ask for permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
}
}
// Formatter
SimpleDateFormat dayformatter = new SimpleDateFormat("EEEE, HH:mm:ss");
try {
timeSet = dayformatter.parse(dayTime);
} catch (ParseException e) {
throw new RuntimeException(e);
}
Log.d("Date Formatter", "" + timeSet + " n" + date1);
}
// Notification Function
public void makeNotification() {
String chanelID = "CHANNEL_ID_NOTIFICATION";
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), chanelID);
builder.setSmallIcon(R.drawable.ic_access_alarm_24)
.setContentTitle("Medicine Reminder")
.setContentText(String.format("Hey! It's time to take your medicine! %s", medicineName))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", medicineName);
// When notification is clicked
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, PendingIntent.FLAG_MUTABLE);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// When the android version is higher than OREO, Set the Channel ID
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(chanelID);
if (notificationChannel == null) {
int importance = NotificationManager.IMPORTANCE_HIGH;
notificationChannel = new NotificationChannel(chanelID, "Some Description", importance);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
}
if(date1.after(timeSet)) {
Log.d("user alarm", "Ring");
notificationManager.notify(0, builder.build());
running = false;
}
}
}
- Make the code send the user notification even though app is closed
- Can you also explain it how it works easily please
New contributor
user20097957 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.