I'm trying to create countdown inside FirebaseMessagingService like this:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
CountDownTimer countDownTimer;
long timerLeft = 5000;
public void onMessageReceived(RemoteMessage remoteMessage) {
startTimer();
}
public void startTimer() {
countDownTimer = new CountDownTimer(timerLeft, 1000) {
public void onTick(long millisUntilFinished) {
timerLeft = millisUntilFinished;
Log.d("timerleft: ",""+timerLeft);
}
public void onFinish() {
timerLeft=10000;
}
}.start();
}
But when I recieve message I have this error:
java.lang.RuntimeException: Can't create handler inside thread Thread[Firebase-Messaging-Intent-Handle,5,main] that has not called Looper.prepare()
How can I resolve?
you can always listen to the log and call
Looper.prepare()
before creatingnew CountDownTimer
, but I would advise you to use some scheduling mechanism, e.g.WorkManager
orAlarmManager
. that depends on what do you want to achieve after counter finishedit due to comments:
FiveSecsBroadcast
is aBroadcastReceiver
which will fire after 5 secs (onReceive
method). you may pass some data usingBundle
extras inIntent