Last active
July 19, 2018 14:45
-
-
Save RichardSilveira/2b5741db32a34e76582743d559f86287 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Senha informada e com suporte para push notification | |
if (!sessionStorage.getItem('notificacao-push-permissao')) { | |
/* Apenas no 1* acesso do usuário a essa funcionalidade, deverá ser exibida e mensagem de Permitir, caso ele tenha permitido | |
/ e posteriormente bloqueado, ele já terá visto essa mensagem, caso ele tenha apagado os dados do localStorage | |
/ (apenas usuários avançados o farão) essa mensagem irá aparecer, mas como ele já terá permitido a notificação, a mensagem nativa | |
/ do browser não será exibida (UX ruim, mas dificilmente irá ocorrer) | |
/ * no iOS o localStorage é apagado periodicamente, porém, como não têm suporte à push notification, sem problemas... */ | |
this.props.messageService.show('Click \'ALLOW\' to be notified of your password, even with your phone in your pocket!') | |
} | |
console.log('@@@ empresa', empresa) | |
console.log('@@@ oneSignalIsInitialized', oneSignalIsInitialized) | |
if (!oneSignalIsInitialized) { | |
this.setState({oneSignalIsInitialized: true}) | |
//const oneSignalAppId = 'xxx-e2c7-4956-bea3-xxx' //PRODUÇÃO | |
//const oneSignalAppId = 'yyy-9aea-4407-b866-yyy' //DESENVOLVIMENTO | |
OneSignal.push(() => { | |
OneSignal.init({ | |
appId: 'xxx-e2c7-4956-bea3-xxx', | |
autoRegister: true, | |
notifyButton: { | |
enable: false, | |
}, | |
welcomeNotification: { | |
disable: true | |
} | |
}) | |
}) | |
} | |
// Code always executed | |
OneSignal.push(() => { | |
OneSignal.sendTags({ | |
estado: empresa.ufDescricao, | |
cidade: empresa.cidadeDescricao, | |
codigo: empresa.codigo, | |
nome: empresa.nomeFantasia | |
}), | |
OneSignal.on('notificationPermissionChange', (permissionChange) => { | |
this.currentPermissionForPush = permissionChange.to | |
// Checking if user previously blocked and allowed notifications now, he will need to refresh the browser (F5) | |
if (sessionStorage.getItem('notificacao-push-permissao') && | |
sessionStorage['notificacao-push-permissao'] === 'denied' && | |
this.currentPermissionForPush === 'granted') { | |
this.props.messageService.show('Agora sim! Mas para ser alertado da sua senha, agora é preciso apenas recarregar a página com F5') | |
sessionStorage['notificacao-push-permissao'] = 'granted' | |
} else if (this.currentPermissionForPush === 'granted') { | |
this.props.messageService.show('Agora você pode deixar seu celular de lado e interagir com outras pessoas, como fazíamos antigamente que quando sua senha for chamada, você será notificado!') | |
sessionStorage['notificacao-push-permissao'] = 'granted' | |
} else if (this.currentPermissionForPush === 'denied') { | |
this.props.messageService.show('Você ainda não confia na gente, mas tudo bem, você ainda pode acompanhar o envio de senhas por aqui mesmo, é tudo em tempo real') | |
sessionStorage['notificacao-push-permissao'] = 'denied' | |
} | |
console.log('New permission state:', this.currentPermissionForPush) | |
}), | |
OneSignal.getUserId().then((userId) => { | |
console.log('OneSignal User ID:', userId) | |
localStorage['notificacao-push-player-id'] = userId | |
OneSignal.getNotificationPermission().then(permission => { | |
this.currentPermissionForPush = permission | |
console.log('@@@ currentPermissionForPush:', this.currentPermissionForPush) | |
sessionStorage['notificacao-push-permissao'] = this.currentPermissionForPush | |
if (this.currentPermissionForPush === 'granted') { | |
this.props.messageService.show('Assim que sua senha for chamada, você será notificado! Pode deixar seu celular de lado e interagir com outras pessoas, como fazíamos antigamente') | |
setTimeout(() => { | |
if (this.addToHomeScreenEvent) | |
this.showPromptAddToHomeScreen() | |
}, 1200) | |
this.inscricoesCollection.add({ | |
playerID: userId, | |
shortDate: currentaISODate, | |
senha: this.state.ultimaSenhaInformaPeloUsuario | |
}).then(docRef => { | |
console.log('Document written with ID: ', docRef.id) | |
this.setState({firestoreDocIDUltimaSenhaInscrita: docRef.id}) | |
}).catch(error => console.error('Error adding document: ', error)) | |
} else if (this.currentPermissionForPush === 'denied') { | |
this.props.messageService.show('Você bloqueou nossas notificações, mas ainda assim pode acompanhar o envio de senhas em tempo real, por aqui mesmo') | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment