Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.controller.managedbean; |
| 2 | |||
| 3 | import java.io.File; |
||
| 4 | import java.io.FileInputStream; |
||
| 5 | import java.io.FileNotFoundException; |
||
| 6 | import java.io.IOException; |
||
| 7 | import java.io.Serializable; |
||
| 8 | |||
| 9 | import javax.annotation.PostConstruct; |
||
| 10 | import javax.inject.Inject; |
||
| 11 | import javax.inject.Named; |
||
| 12 | |||
| 13 | import org.springframework.context.annotation.Scope; |
||
| 14 | import org.springframework.core.io.DefaultResourceLoader; |
||
| 15 | |||
| 16 | import com.google.auth.oauth2.GoogleCredentials; |
||
| 17 | import com.google.firebase.FirebaseApp; |
||
| 18 | import com.google.firebase.FirebaseOptions; |
||
| 19 | import com.google.firebase.auth.FirebaseAuth; |
||
| 20 | import com.google.firebase.database.FirebaseDatabase; |
||
| 21 | import com.google.firebase.messaging.FirebaseMessaging; |
||
| 22 | import com.google.firebase.messaging.FirebaseMessagingException; |
||
| 23 | import com.google.firebase.messaging.Message; |
||
| 24 | |||
| 25 | import br.com.ec.domain.service.seguranca.ContextoSeguranca; |
||
| 26 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 27 | |||
| 28 | @Named |
||
| 29 | @Scope("view") |
||
| 30 | public class NotificacaoBean implements Serializable { |
||
| 31 | |||
| 32 | private static final long serialVersionUID = 1L; |
||
| 33 | |||
| 34 | private ContextoSeguranca contextoSeguranca; |
||
| 35 | |||
| 36 | @Inject |
||
| 37 | public NotificacaoBean(ContextoSeguranca contextoSeguranca) { |
||
| 38 | this.contextoSeguranca = contextoSeguranca; |
||
| 39 | } |
||
| 40 | |||
| 41 | @PostConstruct |
||
| 42 | public void preCarregamento() { |
||
| 43 | // inicializarAppNotificacao(); |
||
| 44 | } |
||
| 45 | |||
| 46 | public FirebaseApp inicializarAppNotificacao() { |
||
| 47 | FileInputStream serviceAccount; |
||
| 48 | FirebaseOptions options; |
||
| 49 | |||
| 50 | try { |
||
| 51 | String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Certificado.CAMINHO_SCHEMAS).getFile().getAbsolutePath() + System.getProperty("file.separator"); |
||
| 52 | serviceAccount = new FileInputStream(caminhoChaves); |
||
| 53 | options = new FirebaseOptions.Builder() |
||
| 54 | .setCredentials(GoogleCredentials.fromStream(serviceAccount)) |
||
| 55 | .setDatabaseUrl("https://project-963735578116.firebaseio.com/") |
||
| 56 | .build(); |
||
| 57 | // .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/") |
||
| 58 | |||
| 59 | return FirebaseApp.initializeApp(options); |
||
| 60 | } catch (Exception e) { |
||
| 61 | e.printStackTrace(); |
||
| 62 | } |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 | |||
| 66 | public void enviarMensagem() { |
||
| 67 | // Initialize the default app |
||
| 68 | FirebaseApp defaultApp = inicializarAppNotificacao(); |
||
| 69 | |||
| 70 | System.out.println(defaultApp.getName()); // "[DEFAULT]" |
||
| 71 | |||
| 72 | // Retrieve services by passing the defaultApp variable... |
||
| 73 | FirebaseAuth defaultAuth = FirebaseAuth.getInstance(defaultApp); |
||
| 74 | FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp); |
||
| 75 | |||
| 76 | // ... or use the equivalent shorthand notation |
||
| 77 | defaultAuth = FirebaseAuth.getInstance(); |
||
| 78 | defaultDatabase = FirebaseDatabase.getInstance(); |
||
| 79 | |||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | public void send(String idPush, String msg) { |
||
| 84 | try { |
||
| 85 | String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Notificacao.CAMINHO_CHAVES).getFile().getAbsolutePath() + System.getProperty("file.separator"); |
||
| 86 | GoogleCredentials fromStream = GoogleCredentials.fromStream(new FileInputStream(new File(caminhoChaves))); |
||
| 87 | FirebaseOptions firebaseOptions = new FirebaseOptions.Builder().setConnectTimeout(10000).setCredentials(fromStream).build(); |
||
| 88 | FirebaseApp firebaseApp = FirebaseApp.initializeApp(firebaseOptions); |
||
| 89 | FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp); |
||
| 90 | Message message = Message.builder().putData("msg", msg).setToken(idPush).build(); |
||
| 91 | String response = firebaseMessaging.send(message); |
||
| 92 | System.out.println(response); |
||
| 93 | } catch (FirebaseMessagingException e) { |
||
| 94 | e.printStackTrace(); |
||
| 95 | } catch (IOException e) { |
||
| 96 | e.printStackTrace(); |
||
| 97 | } |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | /*******************/ |
||
| 102 | |||
| 103 | public FirebaseApp inicializarServicoMensagem() { |
||
| 104 | try { |
||
| 105 | String caminhoChaves = new DefaultResourceLoader().getResource(ConstantesSEC.NotaFiscal.Notificacao.CAMINHO_CHAVES).getFile().getAbsolutePath() + System.getProperty("file.separator"); |
||
| 106 | FileInputStream serviceAccount = new FileInputStream(caminhoChaves); |
||
| 107 | |||
| 108 | FirebaseOptions options = new FirebaseOptions.Builder() |
||
| 109 | .setCredentials(GoogleCredentials.fromStream(serviceAccount)) |
||
| 110 | .setDatabaseUrl("https://espacocaseenvio.firebaseio.com") |
||
| 111 | .build(); |
||
| 112 | |||
| 113 | return FirebaseApp.initializeApp(options); |
||
| 114 | } catch (Exception e) { |
||
| 115 | e.printStackTrace(); |
||
| 116 | } |
||
| 117 | return null; |
||
| 118 | } |
||
| 119 | |||
| 120 | public void enviarMensagem(String idPush) { |
||
| 121 | FirebaseApp firebaseApp = inicializarAppNotificacao(); |
||
| 122 | FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp); |
||
| 123 | |||
| 124 | Message message = Message.builder().putData("msg", "teste BRUNO").setToken(idPush).build(); |
||
| 125 | String response; |
||
| 126 | try { |
||
| 127 | response = firebaseMessaging.send(message); |
||
| 128 | System.out.println(response); |
||
| 129 | } catch (FirebaseMessagingException e) { |
||
| 130 | e.printStackTrace(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | } |