aboutsummaryrefslogtreecommitdiffstats
path: root/site/public/firebase-messaging-sw.js
blob: 056ce2b73002650491c3f2e5b777ba1c3d2fb70b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-messaging-compat.js');

firebase.initializeApp({
  apiKey: "AIzaSyAkxH71PlZJxhD7vuN_Q8kn3TtNnB09_cU",
  authDomain: "updates-9eab8.firebaseapp.com",
  projectId: "updates-9eab8",
  storageBucket: "updates-9eab8.firebasestorage.app",
  messagingSenderId: "347275855103",
  appId: "1:347275855103:web:fb59a7504792c2736538ca"
});

const messaging = firebase.messaging();

// Handle background messages
messaging.onBackgroundMessage(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message', payload);
  
  // Extract notification data
  const notificationTitle = payload.notification?.title || 'New Update';
  const notificationBody = payload.notification?.body || 'You have a new notification';
  
  // Build notification options with enhanced features
  const notificationOptions = {
    body: notificationBody,
    icon: payload.notification?.icon || '/android/android-launchericon-192-192.png',
    badge: '/android/android-launchericon-72-72.png',
    vibrate: [200, 100, 200],
    tag: payload.data?.tag || 'default-tag',
    requireInteraction: payload.data?.requireInteraction === 'true',
    renotify: true,
    silent: false,
    timestamp: Date.now(),
    data: {
      url: payload.data?.url || '/',
      gameId: payload.data?.gameId,
      ...payload.data
    }
  };

  // Add image if provided
  if (payload.notification?.image) {
    notificationOptions.image = payload.notification.image;
  }

  // Add actions if provided
  if (payload.data?.actions) {
    try {
      notificationOptions.actions = JSON.parse(payload.data.actions);
    } catch (e) {
      console.error('Failed to parse notification actions:', e);
    }
  }

  // Show the notification
  return self.registration.showNotification(notificationTitle, notificationOptions);
});

// Handle notification clicks
self.addEventListener('notificationclick', function(event) {
  console.log('[firebase-messaging-sw.js] Notification click received.', event);
  
  event.notification.close();

  // Handle action clicks
  if (event.action) {
    console.log('Action clicked:', event.action);
    // You can handle different actions here
    if (event.action === 'view') {
      event.waitUntil(
        clients.openWindow(event.notification.data?.url || '/')
      );
    } else if (event.action === 'dismiss') {
      // Just close the notification
      return;
    }
  } else {
    // Default click behavior - open the URL
    const clickUrl = event.notification.data?.url || '/';
    
    event.waitUntil(
      clients.matchAll({
        type: 'window',
        includeUncontrolled: true
      }).then(function(clientList) {
        // Check if there's already a window/tab open with the target URL
        for (const client of clientList) {
          if (client.url === clickUrl && 'focus' in client) {
            return client.focus();
          }
        }
        // If no existing window/tab, open a new one
        if (clients.openWindow) {
          return clients.openWindow(clickUrl);
        }
      })
    );
  }
});

// Handle notification close
self.addEventListener('notificationclose', function(event) {
  console.log('[firebase-messaging-sw.js] Notification was closed', event);
  // You can track notification dismissals here if needed
});

// Handle service worker installation
self.addEventListener('install', function(event) {
  console.log('[firebase-messaging-sw.js] Service Worker installing.');
  self.skipWaiting();
});

// Handle service worker activation
self.addEventListener('activate', function(event) {
  console.log('[firebase-messaging-sw.js] Service Worker activated.');
  event.waitUntil(clients.claim());
});

// Handle push events (for debugging)
self.addEventListener('push', function(event) {
  console.log('[firebase-messaging-sw.js] Push event received', event);
  
  if (event.data) {
    try {
      const data = event.data.json();
      console.log('[firebase-messaging-sw.js] Push data:', data);
    } catch (e) {
      console.log('[firebase-messaging-sw.js] Push data text:', event.data.text());
    }
  }
});

// Error handling
self.addEventListener('error', function(event) {
  console.error('[firebase-messaging-sw.js] Service Worker error:', event);
});

// Fetch event handler for offline support (optional)
self.addEventListener('fetch', function(event) {
  // You can add offline caching strategies here if needed
  // For now, just pass through the request
  return;
});
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage