File Manager
Editing: cordova-bridge.php
<?php // Ensure session is available regardless of what the including page has done if (session_status() === PHP_SESSION_NONE) { session_start(); } $bridge_user_id = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null; // Cordova Bridge — included on every page before </body> // Handles splash screen, FCM token capture, and notification permission // Pass user ID from PHP session to JS safely // Outputs null if user is not logged in (guest) $bridge_user_id = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null; ?> <script> (function () { 'use strict'; // Only runs inside the Cordova app — ignored in normal browsers if (!window.FirebasePlugin) return; var TOKEN_ENDPOINT = 'https://www.eizononline.com/api/save-fcm-token.php'; var TOKEN_KEY = 'eizon_fcm_token'; // User ID injected from PHP session — null if guest var USER_ID = <?php echo $bridge_user_id !== null ? $bridge_user_id : 'null'; ?>; // ── Splash screen ───────────────────────────────────────────────────────── if (navigator.splashscreen) { navigator.splashscreen.hide(); } // ── Wait for deviceready then init notifications ─────────────────────────── // deviceready may already have fired since this script runs after page load // so we check both possibilities function initNotifications() { if (!window.FirebasePlugin) { console.warn('[EIZON] FirebasePlugin not available.'); return; } console.log('[EIZON] Initializing notifications. User ID:', USER_ID); // Check permission and capture token FirebasePlugin.hasPermission(function (hasPermission) { if (hasPermission) { getToken(); } else { // Request permission — immediate for test phase FirebasePlugin.requestPermission({ forceShow: true }, function (granted) { if (granted) { console.log('[EIZON] Permission granted.'); getToken(); } else { console.warn('[EIZON] Permission denied.'); } }); } }); // Listen for token refresh FirebasePlugin.onTokenRefresh(function (newToken) { console.log('[EIZON] Token refreshed.'); localStorage.setItem(TOKEN_KEY, newToken); saveToken(newToken); }, function (err) { console.error('[EIZON] Token refresh error:', err); }); // Listen for notifications while app is in foreground FirebasePlugin.onMessageReceived(function (message) { console.log('[EIZON] Message received:', JSON.stringify(message)); if (message.tap) { handleNotificationTap(message); } }); } function getToken() { FirebasePlugin.getToken(function (token) { if (!token) return; console.log('[EIZON] FCM Token:', token); localStorage.setItem(TOKEN_KEY, token); saveToken(token); }, function (err) { console.error('[EIZON] getToken error:', err); }); } function saveToken(token) { var payload = { fcm_token: token, platform: 'android' }; // Attach user ID if logged in if (USER_ID) { payload.user_id = USER_ID; } fetch(TOKEN_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) .then(function (res) { return res.json(); }) .then(function (data) { console.log('[EIZON] Token saved:', JSON.stringify(data)); }) .catch(function (err) { console.error('[EIZON] Failed to save token:', err); }); } function handleNotificationTap(message) { var type = message.notification_type || message.type || ''; var productId = message.product_id || ''; switch (type) { case 'product_request': if (productId) { window.location.href = 'https://www.eizononline.com/product?id=' + productId; } break; case 'daily_reminder': window.location.href = 'https://www.eizononline.com/products'; break; default: break; } } // Handle both cases: script runs before or after deviceready if (document.readyState === 'complete' && window.FirebasePlugin) { initNotifications(); } else { document.addEventListener('deviceready', initNotifications, false); } })(); </script>
💾 Save
⬅ Back