Browse Source

Fix geocache message notifications for cross-device alerts

- Fixed notification not triggering when adding message from another device
- Now notifies ALL devices (including your own) when near a cache with new message
- Added null checks for messages array to prevent errors
- Notifications work for messages from same user on different devices

This fixes the issue where adding a message from desktop wouldn't
notify your phone even when nearby.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
master
HikeMap User 1 month ago
parent
commit
0b0a49c201
  1. 10
      geocaches.json
  2. 13
      index.html

10
geocaches.json

@ -47,6 +47,16 @@
"author": "Riker",
"text": "Stupid little cousin ibby dibby...",
"timestamp": 1767133204544
},
{
"author": "Presley",
"text": "BARK! god damnit BARK!",
"timestamp": 1767199495387
},
{
"author": "test",
"text": "test",
"timestamp": 1767199518737
}
],
"createdAt": 1767127362829,

13
index.html

@ -2552,19 +2552,22 @@
break;
case 'geocacheUpdate':
// Another user added or updated a geocache
// Another user or device added or updated a geocache
if (data.geocache) {
const existingIndex = geocaches.findIndex(g => g.id === data.geocache.id);
if (existingIndex >= 0) {
// Check if this is a new message from another user
// Check if this is a new message
const oldCache = geocaches[existingIndex];
const newMessagesCount = data.geocache.messages.length - oldCache.messages.length;
const oldMessageCount = oldCache.messages ? oldCache.messages.length : 0;
const newMessageCount = data.geocache.messages ? data.geocache.messages.length : 0;
const newMessagesCount = newMessageCount - oldMessageCount;
// Update existing geocache
geocaches[existingIndex] = data.geocache;
// Send notification if new message added by another user and we're nearby
if (newMessagesCount > 0 && userLocation) {
// Send notification if new message added and we're nearby
// This will notify even if it's from your own account on another device
if (newMessagesCount > 0 && userLocation && pushSubscription) {
const distance = L.latLng(userLocation.lat, userLocation.lng)
.distanceTo(L.latLng(data.geocache.lat, data.geocache.lng));

Loading…
Cancel
Save