Fail: Auth Firebase (FCM) Java

0

When attempting to authenticate to Firebase for push notification registration you are returning these messages;

Jar's:  firebase-admin-6.2.0.jar  google-auth-library-oauth2-http-0.11.0.jar  google-oauth-client-1.25.0.jar

  
    

java.lang.ClassNotFoundException: com.google.api.client.googleapis.util.Utils

  

Code

package pmcg.imti.util;

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Collections;

import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.FirebaseMessagingException;

public class PushNotification {

/*
 * Possibilitar a criação de push notifications através do gestor
 */
String cPath = this.getClass().getClassLoader().getResource("../google-services.json").getPath();
File file = new File(cPath);
private InputStream resourceInputStream;
private void PushNotification() {
}

// https://console.firebase.google.com/project/fala-campo-grande/experiments/create/notification/
// https://firebase.google.com/docs/admin/setup?hl=pt-br
public void initialize() {
    /*FileInputStream serviceAccount;
    try {
        String cPath = this.getClass().getClassLoader().getResource("../google-services.json").getPath();

        File file = new File(cPath);
        FileInputStream f = new FileInputStream(file);
        //FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(f))
                FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.getApplicationDefault())
                .setDatabaseUrl("https://fala-campo-grande.firebaseio.com").build();
        FirebaseApp.initializeApp(options);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();

    }*/

}



public String obterAccessToken() throws IOException {
    resourceInputStream = new FileInputStream(this.file);
    GoogleCredentials googleCredential = GoogleCredentials.fromStream(resourceInputStream)
        .createScoped(Collections.singletonList("https://www.googleapis.com/auth/firebase.messaging"));
    googleCredential.refreshAccessToken();
    return googleCredential.getAccessToken().toString();
}


public void iniciarFirebase() throws IOException {
    resourceInputStream = new FileInputStream(this.file);
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(resourceInputStream))
        .build();
        FirebaseApp.initializeApp(options);
}

}

** Call class **

package pmcg.imti.view;

import java.io.IOException;

import com.google.firebase.messaging.FirebaseMessagingException;

import pmcg.framework.ui.WindowCrudCustom; import pmcg.imti.domain.NotPr; import pmcg.imti.util.PushNotification;

public class NotPrCad extends WindowCrudCustom {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
public void initComponentes() {
    PushNotification push = new PushNotification();
    try {
        //push.obterAccessToken();
        push.iniciarFirebase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }//("teste", "msg");

}

@Override
public void preencheParansRel() {
    // TODO Auto-generated method stub

}

}

    
asked by anonymous 27.08.2018 / 22:46

0 answers