79098043

Date: 2024-10-17 12:24:47
Score: 2
Natty:
Report link

I also face same issue, So I change my logic like this

 private String token;
    private static Date tokenExpiryTime;

     private String generateFCMToken(){
            try {
                if (!StringUtils.isEmpty(token) && tokenExpiryTime.after(new Date())) {
                    return token;
                } else {
                    InputStream inputStream = SessionServiceImpl.class.getClassLoader()
                            .getResourceAsStream("PUT_YOUR_FIREBASE_FCM_KEY_FILE");
    
                    GoogleCredential googleCred = GoogleCredential.fromStream(inputStream);
                    GoogleCredential scoped = googleCred.createScoped(Collections.singleton("YOUR_FIREBASE_MESSAGING_URL"));
    
                    scoped.refreshToken();
                    token = scoped.getAccessToken();
                    tokenExpiryTime = new DateTime().plusMinutes("PUT_YOUR_PREFERABLE_TIME_LESS_THAN_OR_EQUAL_TO_60_MIN").toDate();
                    return token;
                }
            } catch (IOException e){
                e.printStackTrace();
                return null;
            }
        }

In this I am storing token generate timing with in addition some minutes, then I am checking if generating time less than current time then I am generating new token.

Hope this is work for you.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I also face same issue
  • Low reputation (1):
Posted by: Dev Vaghasiya