Notifiche push Firebase: da sito web ad app Flutter
Questa guida spiega come integrare le notifiche in un’app Wrapply: notifiche locali, Firebase o OneSignal, richiesta permessi, sincronizzazione del token dispositivo e invio dal backend.
Comandi bridge per notifiche
Questi sono i comandi reali esposti dall'app Wrapply generata. Usali solo dopo che il bridge è pronto e mantieni sempre un fallback per il sito aperto da browser normale.
| Comando | Uso | Payload | Risposta tipica |
|---|---|---|---|
window.Wrapply.notifications.requestPermission(payload) | Richiede il permesso nativo per le notifiche | { provider: 'firebase' } | { ok, granted, permission } |
window.Wrapply.notifications.getToken(payload) | Legge il token dispositivo per il provider selezionato | { provider: 'firebase' } | { ok, token, selectedProvider } |
window.Wrapply.notifications.refreshToken(payload) | Forza l'aggiornamento del token | { provider: 'firebase' } | { ok, token } |
window.Wrapply.notifications.updateToken(payload) | Alias di refreshToken | { provider } | { ok, token } |
window.Wrapply.notifications.show(payload) | Mostra una notifica locale immediata | { title, body, data } | { ok, shown: true } |
window.Wrapply.notifications.getStatus(payload) | Controlla stato permessi e provider | { provider } | { ok, granted, configured } |
if (!Wrapply.notifications) return;
const permission = await Wrapply.notifications.requestPermission({ provider: 'firebase' });
if (!permission.ok || !permission.granted) return;
const token = await Wrapply.notifications.getToken({ provider: 'firebase' });
await fetch('/api/device-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: token.token, provider: token.selectedProvider })
});
});
title: 'Order updated',
body: 'Your order is ready',
data: JSON.stringify({ url: '/orders/123' })
});
Esempi di integrazione
Puoi usare lo stesso pattern bridge in JavaScript, React, Vue, Angular, PHP/Laravel, WordPress o jQuery. La parte importante è attendere il bridge nativo prima di chiamarlo.
JavaScript
if (!window.Wrapply?.notifications) return;
const permission = await window.Wrapply.notifications.requestPermission({ provider: 'firebase' });
if (!permission.ok || !permission.granted) return;
const result = await window.Wrapply.notifications.getToken({ provider: 'firebase' });
await fetch('/api/wrapply-event', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result)
});
}
Hook React
export function useWrapplyBridge() {
const [ready, setReady] = useState(false);
useEffect(() => {
if (window.Wrapply?.__nativeBridgeReady) return setReady(true);
const onReady = () => setReady(true);
window.addEventListener('wrapplyNativeBridgeReady', onReady);
return () => window.removeEventListener('wrapplyNativeBridgeReady', onReady);
}, []);
return { ready, Wrapply: ready ? window.Wrapply : null };
}
Composable Vue
export function useWrapplyBridge() {
const ready = ref(false);
onMounted(() => {
if (window.Wrapply?.__nativeBridgeReady) ready.value = true;
else window.addEventListener('wrapplyNativeBridgeReady', () => ready.value = true, { once: true });
});
return { ready };
}
Service Angular
export class WrapplyBridgeService {
async callNative(payload = {}) {
if (!window.Wrapply?.notifications) return { ok: false, error: 'Bridge unavailable' };
return window.Wrapply.notifications.getStatus(payload);
}
}
jQuery
if (!window.Wrapply?.notifications) return;
const result = await window.Wrapply.notifications.getStatus({});
$('#wrapply-output').text(JSON.stringify(result, null, 2));
});
CMS / template backend
<script>
window.addEventListener('wrapplyNativeBridgeReady', function () {
document.body.classList.add('wrapply-native-app');
});
</script>
Come funzionano le notifiche nelle app Wrapply
Le notifiche non sono solo un pulsante nell’app. Servono permesso, provider, token dispositivo e di solito un backend o una Cloud Function per inviare messaggi remoti.
| Step | Chiamata bridge | Cosa fa |
|---|---|---|
| 1 | requestPermission | Chiede il permesso prima di mostrare notifiche |
| 2 | getToken | Legge il token local/Firebase/OneSignal e lo invia al backend |
| 3 | refreshToken / updateToken | Aggiorna il token dopo login, logout, cambio account o reinstallazione |
| 4 | show | Mostra una notifica locale immediata dal sito dentro l’app |
| 5 | getStatus | Controlla permesso e configurazione provider prima di mostrare azioni UI |
Provider notifiche
Il sito può scegliere il provider notifiche in ogni chiamata. Wrapply include nell’app generata solo il provider selezionato.
| Provider | Uso | Configurazione richiesta | Nota importante |
|---|---|---|---|
| local | Default sicuro, token locale salvato in shared preferences | Nessun file esterno | Utile per test/sessione; non è un vero token push remoto |
| firebase | Provider Firebase Cloud Messaging | android/app/google-services.json e opzionale ios/Runner/GoogleService-Info.plist | Se mancano i file, il bridge torna ok:false/configured:false |
| onesignal | Provider OneSignal | OneSignal App ID in configurazione/build | Richiede setup OneSignal prima di inviare push remote |
Esempi per tipo di codice
Usa le stesse chiamate bridge da JavaScript, React, Vue, Angular o CMS. La parte importante è attendere wrapplyNativeBridgeReady, chiedere il permesso, leggere il token e inviarlo al backend.
if (window.Wrapply && window.Wrapply.__nativeBridgeReady) {
callback(window.Wrapply);
return;
}
window.addEventListener("wrapplyNativeBridgeReady", () => {
callback(window.Wrapply);
}, { once: true });
}
const permission = await Wrapply.notifications.requestPermission({ provider: "firebase" });
if (!permission.ok || !permission.granted) return;
const token = await Wrapply.notifications.getToken({ provider: "firebase" });
await fetch("/api/device-token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: token.token, provider: token.provider })
});
await Wrapply.notifications.show({
title: "Welcome",
body: "Notifications are enabled for this app.",
data: JSON.stringify({ url: window.location.href })
});
});