Subversion Repositories Integrator Subversion

Rev

Rev 106 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

package br.com.ec.controller.managedbean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;
import org.springframework.core.io.DefaultResourceLoader;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;

import br.com.ec.domain.service.seguranca.ContextoSeguranca;
import br.com.ec.domain.shared.ConstantesSEC;

@Named
@Scope("view")
public class NotificacaoBean implements Serializable {

        private static final long serialVersionUID = 1L;
       
        private ContextoSeguranca contextoSeguranca;

        @Inject
        public NotificacaoBean(ContextoSeguranca contextoSeguranca) {
                this.contextoSeguranca = contextoSeguranca;
        }
       
        @PostConstruct
        public void preCarregamento() {
//              inicializarAppNotificacao();
        }
       
        public FirebaseApp inicializarAppNotificacao() {
                FileInputStream serviceAccount;
                FirebaseOptions options;
               
                try {
                        String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Certificado.CAMINHO_SCHEMAS).getFile().getAbsolutePath() + System.getProperty("file.separator");
                        serviceAccount = new FileInputStream(caminhoChaves);
                        options = new FirebaseOptions.Builder()
                                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                                    .setDatabaseUrl("https://project-963735578116.firebaseio.com/")
                                    .build();
//                                  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
       
                        return FirebaseApp.initializeApp(options);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
       
        public void enviarMensagem() {
                // Initialize the default app
                FirebaseApp defaultApp = inicializarAppNotificacao();

                System.out.println(defaultApp.getName());  // "[DEFAULT]"

                // Retrieve services by passing the defaultApp variable...
                FirebaseAuth defaultAuth = FirebaseAuth.getInstance(defaultApp);
                FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp);

                // ... or use the equivalent shorthand notation
                defaultAuth = FirebaseAuth.getInstance();
                defaultDatabase = FirebaseDatabase.getInstance();
               
               
        }
       
        public void send(String idPush, String msg)  {
        try {
                String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Notificacao.CAMINHO_CHAVES).getFile().getAbsolutePath() + System.getProperty("file.separator");
            GoogleCredentials fromStream = GoogleCredentials.fromStream(new FileInputStream(new File(caminhoChaves)));
            FirebaseOptions firebaseOptions = new FirebaseOptions.Builder().setConnectTimeout(10000).setCredentials(fromStream).build();
            FirebaseApp firebaseApp = FirebaseApp.initializeApp(firebaseOptions);
            FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
            Message message = Message.builder().putData("msg", msg).setToken(idPush).build();
            String response = firebaseMessaging.send(message);
            System.out.println(response);
        } catch (FirebaseMessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
       
        /*******************/
       
        public FirebaseApp inicializarServicoMensagem() {
                try {
                        String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Notificacao.CAMINHO_CHAVES).getFile().getAbsolutePath() + System.getProperty("file.separator");
                        FileInputStream serviceAccount = new FileInputStream(caminhoChaves);

                        FirebaseOptions options = new FirebaseOptions.Builder()
                                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                                .setDatabaseUrl("https://espacocaseenvio.firebaseio.com")
                                .build();

                        return FirebaseApp.initializeApp(options);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
       
        public void enviarMensagem(String idPush) {
                FirebaseApp firebaseApp = inicializarAppNotificacao();
                FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
               
        Message message = Message.builder().putData("msg", "teste BRUNO").setToken(idPush).build();
        String response;
                try {
                        response = firebaseMessaging.send(message);
                        System.out.println(response);
                } catch (FirebaseMessagingException e) {
                        e.printStackTrace();
                }
        }

}