| | "use client"; |
| |
|
| | import { useEffect } from 'react'; |
| | import './globals.css'; |
| | import { Capacitor } from '@capacitor/core'; |
| | import { PushNotifications } from '@capacitor/push-notifications'; |
| | import { Device } from '@capacitor/device'; |
| | import { doc, updateDoc } from "firebase/firestore"; |
| | import { db } from "@/lib/firebase-client"; |
| | import { Providers } from './providers'; |
| |
|
| | export default function RootLayout({ children }: { children: React.ReactNode }) { |
| |
|
| | useEffect(() => { |
| | if (Capacitor.isNativePlatform()) { |
| | |
| | const initializeAppCapabilities = async () => { |
| | |
| | const deviceId = await Device.getId(); |
| | localStorage.setItem('android_id', deviceId.identifier); |
| |
|
| | |
| | let permStatus = await PushNotifications.checkPermissions(); |
| | if (permStatus.receive === 'prompt') { |
| | permStatus = await PushNotifications.requestPermissions(); |
| | } |
| |
|
| | if (permStatus.receive === 'granted') { |
| | await PushNotifications.register(); |
| | } |
| | }; |
| |
|
| | initializeAppCapabilities(); |
| |
|
| | |
| | PushNotifications.addListener('registration', async (token) => { |
| | console.log('Firebase Push Token:', token.value); |
| | |
| | const userId = localStorage.getItem('userId'); |
| |
|
| | if (userId && db) { |
| | const userDocRef = doc(db, 'users', userId); |
| | try { |
| | await updateDoc(userDocRef, { fcmToken: token.value }); |
| | console.log('FCM token successfully saved to Firestore for Android.'); |
| | } catch (error) { |
| | console.error('Error saving FCM token to Firestore:', error); |
| | } |
| | } |
| | }); |
| |
|
| | PushNotifications.addListener('registrationError', (error) => { |
| | console.error('Push Registration Error:', error); |
| | }); |
| | } |
| | }, []); |
| |
|
| | return ( |
| | <html lang="ar" dir="rtl" suppressHydrationWarning> |
| | <body className="font-body antialiased" suppressHydrationWarning> |
| | <Providers>{children}</Providers> |
| | </body> |
| | </html> |
| | ); |
| | } |
| |
|