Skip to content

Commit

Permalink
Init notifiers/catalog/index.ts
Browse files Browse the repository at this point in the history
No type definitions for chrome.instanceID
  • Loading branch information
WebGL3D committed Jun 18, 2023
1 parent e9ec3c2 commit 934f45e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/js/service-worker/notifiers/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getAuthenticatedUser } from '../../../services/users';

declare namespace chrome.instanceID {
export interface TokenRefreshEvent
extends chrome.events.Event<(token: string) => void> {}
}

const updateToken = (): Promise<void> => {
return new Promise(async (resolve, reject) => {
try {
const authenticatedUser = await getAuthenticatedUser();

chrome.instanceID.getToken(
{ authorizedEntity: '303497097698', scope: 'FCM' },
(token: string) => {
fetch('https://api.roblox.plus/v2/itemnotifier/registertoken', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `robloxUserId=${
authenticatedUser?.id
}&token=${encodeURIComponent(token)}`,
})
.then((response) => {
if (response.ok) {
resolve();
} else {
reject();
}
})
.catch(reject);
}
);
} catch (err) {
reject(err);
}
});
};

const processMessage = (message: any) => {
console.log('process message', message);
};

chrome.gcm.onMessage.addListener(processMessage);
chrome.instanceID.onTokenRefresh.addListener(updateToken);

0 comments on commit 934f45e

Please sign in to comment.