Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failing push notifications #25340

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,21 @@ export async function listenToPushNotifications(
const unsubscribePushNotifications = onBackgroundMessage(
messaging,
async (payload: MessagePayload): Promise<void> => {
const typedPayload = payload;

// if the payload does not contain data, do nothing
try {
const notificationData: NotificationUnion = typedPayload?.data?.data
? JSON.parse(typedPayload?.data?.data)
const data = payload?.data?.data
? JSON.parse(payload?.data?.data)
: undefined;

if (!notificationData) {
// if the payload does not contain data, do nothing
if (!data) {
return;
}

const notificationData = {
...data,
type: data?.type ?? data?.data?.kind,
} as NotificationUnion;
Comment on lines +208 to +211
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

God more bad types we need to resolve.

A raw notification DOES NOT have a type property, this is something we inject before processing. This adds back this type property if it is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of our types are sound. These are edge-cases and artefacts from the portfolio integration.


const notification = processNotification(notificationData);
onNewNotification(notification);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import browser from 'webextension-polyfill';

export async function getNotificationImage() {
const iconUrl = await browser.runtime.getURL('../../images/icon-64.png');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This correctly gets the browser image (used on other notifications - e.g. transaction notifications)

return iconUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { t } from '../../../translate';
import type { Notification } from '../../metamask-notifications/types/types';
import ExtensionPlatform from '../../../platforms/extension';
import { getAmount, formatAmount } from './get-notification-data';
import { getNotificationImage } from './get-notification-image';

type PushNotificationMessage = {
title: string;
Expand Down Expand Up @@ -47,9 +48,11 @@ export async function onPushNotification(
return;
}

const iconUrl = await getNotificationImage();

await registration.showNotification(notificationMessage.title, {
body: notificationMessage.description,
icon: './images/icon-64.png',
icon: iconUrl,
tag: notification?.id,
data: notification,
});
Expand Down