I have a singleton class which receives callbacks and handles them, but the class is never called from within the source code. The problem is that Java does not initialize this class because it is not used in the source code, so I will never receive the callbacks. I already thought about calling it manually but I'd prefer it if the class would initialize automatically.
I already tried to use the static initializer, but this is not called either.
Question: what would be the best way to initialize this static class?
private static PushNotificationReceiver receiver = new PushNotificationReceiver();
private PushNotificationReceiver() { }
public static PushNotificationReceiver getInstance() { return receiver; }
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Message is received, do something.
}
Edit 1: I found a solution, but this is just by calling the class manually. Is there really no other better alternative which will do this automatically? https://stackoverflow.com/a/9130560/4653908