-
-
Notifications
You must be signed in to change notification settings - Fork 123
feat(notifications): add AE native notifications #2209
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
Open
riderx
wants to merge
13
commits into
main
Choose a base branch
from
codex/native-notifications-ae
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
154a70f
feat(notifications): add AE native notifications
riderx 7cc3eeb
fix(notifications): address sonar security hotspots
riderx 144bb36
fix(notifications): avoid duplicate plugin export
riderx 6d58182
fix(cli): sort notification setup imports
riderx 9a4c672
fix(db): refresh notification migration timestamp
riderx 8e50ced
fix(notifications): address review feedback
riderx dd15b7d
fix(notifications): satisfy sonar checks
riderx 9bf302b
fix(notifications): set android manifest safety defaults
riderx ee13e04
fix(notifications): require signed notification identities
riderx a9d0c46
ci: use supported node for wrangler tests
riderx 972f806
fix(notifications): address remaining review feedback
riderx 249899a
fix(notifications): handle listener and lookup review issues
riderx f1ff7ab
fix(notifications): refresh migration timestamp
riderx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { existsSync, mkdirSync } from 'node:fs' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { cwd } from 'node:process' | ||
| import { intro, log, outro, spinner } from '@clack/prompts' | ||
| import { formatRunnerCommand, splitRunnerCommand } from '../runner-command' | ||
| import { defaultApiHost, formatError, getConfig, getPMAndCommand, updateConfigbyKey } from '../utils' | ||
| import { writeFileAtomic } from '../utils/safeWrites' | ||
|
|
||
| const notificationPackages = [ | ||
| '@capgo/capacitor-notifications', | ||
| '@capacitor/push-notifications', | ||
| '@capacitor/preferences', | ||
| '@capacitor/app', | ||
| '@capacitor/device', | ||
| ] | ||
|
|
||
| interface NotificationSetupOptions { | ||
| serverUrl?: string | ||
| file?: string | ||
| force?: boolean | ||
| install?: boolean | ||
| sync?: boolean | ||
| } | ||
|
|
||
| function getConfigAppId(config: Awaited<ReturnType<typeof getConfig>>) { | ||
| return String(config.config?.plugins?.CapacitorUpdater?.appId || config.config?.appId || '') | ||
| } | ||
|
|
||
| function renderNotificationHelper(appId: string, serverUrl: string) { | ||
| const appIdLiteral = JSON.stringify(appId) | ||
| const serverUrlLiteral = JSON.stringify(serverUrl) | ||
|
|
||
| return `import { CapgoNotifications } from '@capgo/capacitor-notifications' | ||
|
|
||
| export interface CapgoNotificationIdentity { | ||
| externalId: string | ||
| identityProof: string | ||
| tags?: string[] | ||
| attributes?: Record<string, unknown> | ||
| consent?: boolean | ||
| } | ||
|
|
||
| export async function setupCapgoNotifications(identity: CapgoNotificationIdentity) { | ||
| if (!identity.externalId) | ||
| return | ||
| if (!identity.identityProof) | ||
| throw new Error('Capgo notification identityProof is required') | ||
|
|
||
| await CapgoNotifications.configure({ | ||
| appId: ${appIdLiteral}, | ||
| serverUrl: ${serverUrlLiteral}, | ||
| }) | ||
|
|
||
| return CapgoNotifications.register({ | ||
| externalId: identity.externalId, | ||
| identityProof: identity.identityProof, | ||
| tags: identity.tags ?? [], | ||
| attributes: identity.attributes ?? {}, | ||
| consent: identity.consent ?? true, | ||
| }) | ||
| } | ||
|
|
||
| export { CapgoNotifications } | ||
| ` | ||
| } | ||
|
|
||
| function runCommand(command: string, args: string[], failureMessage: string) { | ||
| const result = spawnSync(command, args, { stdio: 'inherit' }) | ||
| if (result.error) | ||
| throw result.error | ||
| if (result.status !== 0) | ||
| throw new Error(`${failureMessage} exited with code ${result.status}`) | ||
| } | ||
|
|
||
| function runInstall() { | ||
| const pm = getPMAndCommand() | ||
| log.info(`Installing notification packages with ${pm.installCommand}`) | ||
| runCommand(pm.pm, [pm.command, ...notificationPackages], 'Notification package install') | ||
| } | ||
|
|
||
| function runSync() { | ||
| const pm = getPMAndCommand() | ||
| const runner = splitRunnerCommand(pm.runner) | ||
| const displayCommand = formatRunnerCommand(pm.runner, ['cap', 'sync']) | ||
| log.info(`Running ${displayCommand}`) | ||
| runCommand(runner.command, [...runner.args, 'cap', 'sync'], 'Capacitor sync') | ||
| } | ||
|
|
||
| async function writeHelperFile(filePath: string, appId: string, serverUrl: string, force: boolean | undefined) { | ||
| const absolutePath = resolve(cwd(), filePath) | ||
| if (existsSync(absolutePath) && !force) | ||
| throw new Error(`${filePath} already exists. Re-run with --force to overwrite it.`) | ||
|
|
||
| mkdirSync(dirname(absolutePath), { recursive: true }) | ||
| await writeFileAtomic(absolutePath, renderNotificationHelper(appId, serverUrl), { mode: 0o644 }) | ||
| return absolutePath | ||
| } | ||
|
|
||
| export async function setupNotifications(appIdArg: string | undefined, options: NotificationSetupOptions) { | ||
| intro('Capgo native notifications setup') | ||
| const progress = spinner() | ||
|
|
||
| try { | ||
| const config = await getConfig() | ||
| const appId = appIdArg || getConfigAppId(config) | ||
| if (!appId) | ||
| throw new Error('Missing appId. Pass it as `notifications setup com.example.app` or set it in capacitor.config.') | ||
|
|
||
| const serverUrl = options.serverUrl || defaultApiHost | ||
| const helperFile = options.file || 'src/capgo-notifications.ts' | ||
|
|
||
| if (options.install !== false) | ||
| runInstall() | ||
|
|
||
| progress.start('Saving Capacitor notification config') | ||
| await updateConfigbyKey('CapgoNotifications', { appId, serverUrl }) | ||
| progress.stop('Capacitor notification config saved') | ||
|
|
||
| const writtenPath = await writeHelperFile(helperFile, appId, serverUrl, options.force) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| log.success(`Created ${writtenPath}`) | ||
|
|
||
| if (options.sync !== false) | ||
| runSync() | ||
|
|
||
| log.info('Import setupCapgoNotifications(...) after your user is known, and pass your stable customer external ID.') | ||
| log.info('Then configure FCM/APNs in the Capgo app Notifications tab before sending production notifications.') | ||
| outro('Notifications setup done') | ||
| } | ||
| catch (error) { | ||
| progress.stop('Notifications setup failed') | ||
| log.error(formatError(error)) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export { renderNotificationHelper } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.