DEV Community

Vitor Freitas
Vitor Freitas

Posted on

2

Ionic + OneSignal: Utilizando na web e no mobile.

Contexto

Estou trabalhando em uma aplicação mobile feita em Ionic, que precisou ser portada também para web. A versão antiga dessa aplicação (web) era feita em Angular 1 e, com o objetivo de melhorar a experiência e reduzir o custo da manutenção, resolvemos utilizar a base de código do nosso app também para web.

O problema

Para notificar no mobile usamos o @ionic-native/onesignal e na web o OneSignalWebSDK. Tudo funcionava bem com um simples ionic serve. Entretanto, após fazer um build com ionic cordova build browser, o OneSignal que era injetado no objeto window não era aquele provido pelo OneSignalWebSDK, e sim o do cordova. Cheguei nessa conclusão após analisar vários console.logs e ver diferenças marcantes entre o output dos logs de desenvolvimento e de produção.

A solução

Para solucionar precisei importar o OneSignalWebSDK programaticamente. Isto é, usando JavaScript ao invés de uma tag <script> no arquivo index.html. Este chunk de código é executado após o cordova carregar todos os seus plugins, ou seja, após a Promise platform.ready() ser resolvida.

O código

app/app.component.ts
app.component.ts

providers/notification/notification.ts
notification.ts

notification.ts

notification.ts

Problem Solved

Resolvi omitir a implementação do OneSignal do cordova pois na documentação contém o necessário para sair usando. Espero que isto resolva o seu problema assim como resolveu o meu. Obrigado!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay