Skip to content

Commit

Permalink
feat: support ios native push (#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoshamun authored Jan 16, 2025
1 parent 81866f8 commit 04930ba
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/onesignal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as OneSignal from '@onesignal/node-onesignal';
import { NotificationV2, NotificationAvatarV2 } from './entity';
import { addNotificationUtm, basicHtmlStrip, mapCloudinaryUrl } from './common';
import { escapeRegExp } from 'lodash';

const appId = process.env.ONESIGNAL_APP_ID;
const apiKey = process.env.ONESIGNAL_API_KEY;
const safeCommentsPrefix = escapeRegExp(process.env.COMMENTS_PREFIX || '');

const configuration = OneSignal.createConfiguration({
appKey: apiKey,
Expand All @@ -15,6 +17,30 @@ const chromeWebBadge =
const chromeWebIcon =
'https://media.daily.dev/image/upload/s--9vc188bS--/f_auto/v1712221649/1_smcxpz';

function createPush(
userIds: string[],
url: string | undefined,
notificationType: string,
): OneSignal.Notification {
const push = new OneSignal.Notification();
push.app_id = appId;
push.include_external_user_ids = userIds;
push.chrome_web_badge = chromeWebBadge;
push.chrome_web_icon = chromeWebIcon;
push.ios_badge_type = 'Increase';
push.ios_badge_count = 1;

if (url) {
push.web_url = addNotificationUtm(url, 'push', notificationType);
push.app_url = push.web_url.replace(
new RegExp(`${safeCommentsPrefix}/?`),
'dailydev://',
);
}

return push;
}

export async function sendPushNotification(
userIds: string[],
{
Expand All @@ -27,14 +53,10 @@ export async function sendPushNotification(
): Promise<void> {
if (!appId || !apiKey) return;

const push = new OneSignal.Notification();
push.app_id = appId;
push.include_external_user_ids = userIds;
const push = createPush(userIds, targetUrl, type);
push.contents = { en: basicHtmlStrip(title) };
push.headings = { en: 'New update' };
push.url = addNotificationUtm(targetUrl, 'push', type);
push.data = { notificationId: id };
push.chrome_web_badge = chromeWebBadge;
if (avatar) {
push.chrome_web_icon = mapCloudinaryUrl(avatar.image);
}
Expand Down Expand Up @@ -73,48 +95,36 @@ export async function sendReadingReminderPush(

const seed = Math.floor(new Date().getTime() / 1000);

const push = new OneSignal.Notification();
push.app_id = appId;
push.include_external_user_ids = userIds;
const push = createPush(
userIds,
`${process.env.COMMENTS_PREFIX}`,
'reminder',
);
push.send_after = at.toISOString();
push.contents = {
en: readingReminderContents[seed % readingReminderContents.length],
};
push.headings = {
en: readingReminderHeadings[seed % readingReminderHeadings.length],
};
push.url = addNotificationUtm(
process.env.COMMENTS_PREFIX,
'push',
'reminder',
);
push.chrome_web_badge = chromeWebBadge;
push.chrome_web_icon = chromeWebIcon;
await client.createNotification(push);
}

export async function sendStreakReminderPush(
userIds: string[],
): Promise<null | OneSignal.CreateNotificationSuccessResponse> {
if (!appId || !apiKey) return null;
const push = new OneSignal.Notification();
push.app_id = appId;
push.include_external_user_ids = userIds;
const push = createPush(
userIds,
process.env.COMMENTS_PREFIX,
'streak_reminder',
);
push.contents = {
en: streakReminderContent,
};
push.headings = {
en: streakReminderHeading,
};
push.url = addNotificationUtm(
process.env.COMMENTS_PREFIX,
'push',
'streak_reminder',
);

push.chrome_web_badge = chromeWebBadge;
push.chrome_web_icon = chromeWebIcon;

return await client.createNotification(push);
}

Expand All @@ -130,24 +140,16 @@ export const sendGenericPush = async (
notification: GenericPushPayload,
) => {
if (!appId || !apiKey) return null;
const push = new OneSignal.Notification();
push.app_id = appId;
push.include_external_user_ids = userIds;
const push = createPush(
userIds,
notification.url,
notification.utm_campaign ?? 'generic',
);
push.contents = {
en: notification.body,
};
push.headings = {
en: notification.title,
};

push.chrome_web_badge = chromeWebBadge;
push.chrome_web_icon = chromeWebIcon;

if (notification.url) {
push.url = notification.utm_campaign
? addNotificationUtm(notification.url, 'push', notification.utm_campaign)
: notification.url;
}

return client.createNotification(push);
};

0 comments on commit 04930ba

Please sign in to comment.