diff --git a/desktop/src/push-notifications.js b/desktop/src/push-notifications.js index 73857b508..70c6a79f2 100644 --- a/desktop/src/push-notifications.js +++ b/desktop/src/push-notifications.js @@ -1,46 +1,50 @@ // @flow // eslint-disable-next-line import/extensions import { pushNotifications, Notification } from 'electron/main'; async function registerForNotifications(): Promise { if (process.platform !== 'darwin') { return null; } try { const token = await pushNotifications.registerForAPNSNotifications(); return token; } catch (err) { console.error(err); } return null; } +function showNewNotification( + payload: { +[string]: mixed }, + handleClick: (threadID: string) => void, +) { + if ( + typeof payload.title !== 'string' || + typeof payload.body !== 'string' || + typeof payload.threadID !== 'string' + ) { + return; + } + const { title, body, threadID } = payload; + const notif = new Notification({ + title, + body, + }); + notif.on('click', () => handleClick(threadID)); + notif.show(); +} + function listenForNotifications(handleClick: (threadID: string) => void) { if (process.platform !== 'darwin') { return; } pushNotifications.on('received-apns-notification', (event, userInfo) => { - if ( - typeof userInfo.title !== 'string' || - typeof userInfo.body !== 'string' || - typeof userInfo.threadID !== 'string' - ) { - return; - } - const { title, body, threadID } = userInfo; - - const notif = new Notification({ - title, - body, - }); - notif.on('click', () => { - handleClick(threadID); - }); - notif.show(); + showNewNotification(userInfo, handleClick); }); } export { listenForNotifications, registerForNotifications };