Skip to content

Commit

Permalink
Merge pull request #1119 from near/default_scope
Browse files Browse the repository at this point in the history
Add debug, error logs for push notifications and update the service-worker's scope
  • Loading branch information
charleslavon authored Apr 18, 2024
2 parents b0d31b7 + 39e2e49 commit 77a1cf5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions public/push-notifications.js → public/push/push-notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,33 @@ const getLink = (props) => {
} else if (props.receiver && props.actionAtBlockHeight) {
return `https://near.org/s/p?a=${props.receiver}&b=${props.actionAtBlockHeight}`;
}
return "http://near.org/notifications";
return `https://near.org/notifications`;
};

// TODO: Add error handling if data will not match
function handlePushEvent(event) {
console.log('SW - push event received', event);

const notification = event.data.json();

const { initiatedBy = '', valueType = '' } = notification;
console.log('SW - push event received', notification);

const { initiatedBy, valueType } = notification;

if (!initiatedBy || !valueType) {
console.error(
`handlePushEvent received event with an undefined required value valueType=${valueType}, initiatedBy=${initiatedBy}`,
);
return;
}

const title = getNotificationTitle({ accountId: initiatedBy, notificationType: valueType });
const options = getNotificationOptions({ notificationType: valueType, ...notification });

event.waitUntil(self.registration.showNotification(title, options));
console.log('SW - triggering notification with', title, options);

try {
event.waitUntil(self.registration.showNotification(title, options));
} catch (e) {
console.error('Error during an attempt to show a notification', e);
}
}

const handlePushClick = (event) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const termsDomainName = `${process.env.NEXT_PUBLIC_TOS_SUBDOMAIN_NAME}/ip
export const privacyDomainName = `${process.env.NEXT_PUBLIC_TOS_SUBDOMAIN_NAME}/ipfs/${process.env.NEXT_PUBLIC_PRIVACY_CID}`;
export const notificationApplicationServerKey = process.env.NEXT_PUBLIC_NOTIFICATIONS_APPLICATION_SERVER_KEY;
export const notificationsHostName = process.env.NEXT_PUBLIC_NOTIFICATIONS_HOSTNAME;
export const notificationsGatewayUrl = process.env.NEXT_PUBLIC_NOTIFICATIONS_GATEWAY_URL ?? 'https://near.org';
export const notificationsGatewayUrl = `${process.env.NEXT_PUBLIC_HOSTNAME}`;
export const notificationsLocalStorageKey = 'push-notifications-v0';
export const localStorageAccountIdKey = 'near-social-vm:v01::accountId:';
export const isLocalEnvironment = process.env.NEXT_PUBLIC_LOCAL_ENVIRONMENT === 'true';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const handleRequestPermission = () => {

const registerServiceWorker = () => {
try {
return navigator.serviceWorker.register('/push-notifications.js', { scope: '/notifications-settings' });
return navigator.serviceWorker.register('/push/push-notifications.js');
} catch (e: any) {
const scope = 'Error while registering the push notification service-worker';
console.error(scope, e);
Expand Down

0 comments on commit 77a1cf5

Please sign in to comment.