如何解决 Firebase 云消息传递问题

问题描述 投票:0回答:1

我正在尝试通过 Firebase Cloud Messaging 将推送通知发送到我的手机,但我收到了以下错误,所以您能否帮助解决此问题

enter image description here

public String pushNotificationFCM(ProfileDTO profileDTO, SigningTransactionDTO signingTransactionDTO) {
        FirebaseApp firebaseApp = null;
        try {
          .....
                log.info(Constants.FCM_LOG_MESSAGE + ": proxy is disabled");
                // Loading the resource file from the classpath
                Resource resource = new ClassPathResource(fcmKeyPath);

                // Creating an InputStream from the resource
                try (InputStream inputStream = resource.getInputStream()) {
                    FirebaseOptions options = new FirebaseOptions.Builder()
                            .setCredentials(GoogleCredentials.fromStream(inputStream))
                            .setServiceAccountId(fcmAccountService)
                            .build();


                    if (FirebaseApp.getApps().isEmpty()) {
                        firebaseApp = FirebaseApp.initializeApp(options);
                    } else {
                        firebaseApp = FirebaseApp.getApps().get(0);
                    }

                    log.info("Application initialized without proxy");
                }
            }

        } catch (IOException e) {
            log.error( e.getMessage());
        }

try {
            response = FirebaseMessaging.getInstance(firebaseApp).send(message);
          
        } catch (FirebaseMessagingException e) {
            log.info(" Error " + response);
      
        }
  
java firebase firebase-cloud-messaging
1个回答
0
投票

我也面临同样的问题,所以我像这样改变我的逻辑

 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;
            }
        }

在此,我额外存储了几分钟的令牌生成时间,然后我检查生成时间是否小于当前时间,然后我生成新的令牌

希望这对您有用。

© www.soinside.com 2019 - 2024. All rights reserved.