im trying to get a notification in my app with a timer on it. i tried to watch some guides and get help from GPT and my Service just is not displaying my notfication for some reason
this is my service and for some reason the notification is not working i would be happy to know what did i do worng and how to fix it
package com.example.main_app;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
public class TimerService extends Service {
private static final String TAG = "TimerService";
public static final String TIMER_UPDATED_ACTION = "com.example.main_app.TIMER_UPDATED";
public static final String TIMER_REMAINING_KEY = "timer_remaining";
private static final String CHANNEL_ID = "TimerServiceChannel";
private static final int NOTIFICATION_ID = 1;
private final IBinder binder = new LocalBinder();
private CountDownTimer countDownTimer;
private long timeLeftInMillis;
private boolean timerRunning = false;
public class LocalBinder extends Binder {
TimerService getService() {
return TimerService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Service onBind");
return binder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (timerRunning) {
countDownTimer.cancel();
}
Log.d(TAG, "Service onDestroy");
}
public void startTimer(long durationInMillis) {
if (timerRunning) {
return;
}
Log.d(TAG, "Timer started for: " + durationInMillis);
timeLeftInMillis = durationInMillis;
countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
Log.d(TAG, "Timer onTick: " + millisUntilFinished);
sendTimerUpdate();
updateNotification(millisUntilFinished);
}
@Override
public void onFinish() {
timerRunning = false;
Log.d(TAG, "Timer finished");
sendTimerUpdate();
updateNotification(0);
stopSelf(); // Stop the service when the timer finishes
}
};
countDownTimer.start();
timerRunning = true;
startForeground(NOTIFICATION_ID, getNotification(timeLeftInMillis));
}
public void pauseTimer() {
if (timerRunning) {
countDownTimer.cancel();
timerRunning = false;
Log.d(TAG, "Timer paused");
sendTimerUpdate();
updateNotification(timeLeftInMillis);
}
}
public void stopTimer() {
if (countDownTimer != null) {
countDownTimer.cancel();
}
timerRunning = false;
timeLeftInMillis = 0;
Log.d(TAG, "Timer stopped");
sendTimerUpdate();
stopForeground(true);
stopSelf(); // Stop the service when the timer is manually stopped
}
private void sendTimerUpdate() {
Intent intent = new Intent(TIMER_UPDATED_ACTION);
intent.putExtra(TIMER_REMAINING_KEY, timeLeftInMillis);
sendBroadcast(intent);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Timer Service Channel";
String description = "Channel for Timer Service";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
private Notification getNotification(long millisUntilFinished) {
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Timer Running")
.setContentText("Time remaining: " + millisUntilFinished / 1000 + " seconds")
.setSmallIcon(R.drawable.ic_timer)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setOngoing(true)
.build();
}
private void updateNotification(long millisUntilFinished) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.notify(NOTIFICATION_ID, getNotification(millisUntilFinished));
}
}
this is my service and for some reason the notification is not working i would be happy to know what did i do worng and how to fix it