Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(onesignal): honor email delivery and notification channels

Rudy Celekliaa6550044840
diff --git a/tests/clis/onesignal-send.test.cjs b/tests/clis/onesignal-send.test.cjs
new file mode 100644
index 0000000..3637e95
--- /dev/null
+++ b/tests/clis/onesignal-send.test.cjs
@@ -0,0 +1,68 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/onesignal.js')
+function run(args, network = true) {
+ const code = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected network request');
+ return { status: 200, text: async () => JSON.stringify({ url, headers: options.headers, body: JSON.parse(options.body) }) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, 'notifications', 'send', ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, ONESIGNAL_REST_API_KEY: 'dummy-secret', ONESIGNAL_APP_ID: 'dummy-app' } })
+}
+function sent(args) {
+ const r = run(args); assert.equal(r.status, 0, r.stderr)
+ const p = JSON.parse(r.stdout); assert.ok(!p.error, r.stdout); return p
+}
+test('email addresses receive required email content rather than push contents', () => {
+ const p = sent(['--emails', '[email protected],[email protected]', '--heading', 'Welcome', '--message', '<p>Hello</p>'])
+ assert.equal(p.url, 'https://api.onesignal.com/notifications?c=email')
+ assert.equal(p.headers.Authorization, 'Key dummy-secret')
+ assert.deepEqual(p.body.email_to, ['[email protected]', '[email protected]'])
+ assert.equal(p.body.email_subject, 'Welcome'); assert.equal(p.body.email_body, '<p>Hello</p>')
+ assert.equal(p.body.target_channel, 'email'); assert.ok(!p.body.contents)
+})
+test('explicit email content works without an unrelated push message', () => {
+ const p = sent(['--channel', 'email', '--aliases', 'customer', '--email-subject', 'Receipt', '--email-body', '<p>Paid</p>'])
+ assert.equal(p.body.email_subject, 'Receipt'); assert.equal(p.body.email_body, '<p>Paid</p>')
+ assert.deepEqual(p.body.include_aliases, { external_id: ['customer'] })
+})
+test('segment targeting honors explicit email channel', () => {
+ const p = sent(['--channel', 'email', '--segment', 'Customers', '--heading', 'News', '--message', '<p>Update</p>'])
+ assert.equal(p.body.target_channel, 'email'); assert.deepEqual(p.body.included_segments, ['Customers'])
+ assert.ok(!p.body.contents)
+})
+test('default push preserves headings, link, data, schedule and TTL', () => {
+ const p = sent(['--message', 'Hello', '--heading', 'Hi', '--url', 'https://example.test', '--data', '{"plan":"pro"}', '--send-after', '2026-11-01T00:00:00Z', '--ttl', '60'])
+ assert.equal(p.url, 'https://api.onesignal.com/notifications?c=push')
+ assert.deepEqual(p.body.contents, { en: 'Hello' }); assert.deepEqual(p.body.headings, { en: 'Hi' })
+ assert.deepEqual(p.body.included_segments, ['Subscribed Users']); assert.equal(p.body.url, 'https://example.test')
+ assert.deepEqual(p.body.data, { plan: 'pro' }); assert.equal(p.body.ttl, 60)
+ assert.equal(p.body.send_after, '2026-11-01T00:00:00Z')
+})
+test('SMS aliases retain SMS contents and explicit channel', () => {
+ const p = sent(['--channel', 'sms', '--aliases', '{"external_id":["customer"]}', '--message', 'Order ready'])
+ assert.equal(p.body.target_channel, 'sms'); assert.equal(p.url, 'https://api.onesignal.com/notifications?c=sms')
+ assert.deepEqual(p.body.contents, { en: 'Order ready' })
+})
+test('legacy player IDs retain their target list', () => {
+ const p = sent(['--player-ids', 'subscription-1,subscription-2', '--message', 'Hello'])
+ assert.deepEqual(p.body.include_player_ids, ['subscription-1', 'subscription-2'])
+})
+for (const [name, args, pattern] of [
+ ['missing email subject', ['--emails', '[email protected]', '--message', 'Hello'], /subject/],
+ ['missing email body', ['--channel', 'email', '--aliases', 'customer', '--email-subject', 'Hi'], /body/],
+ ['incompatible email and push channel', ['--emails', '[email protected]', '--channel', 'push', '--message', 'Hello'], /email/],
+ ['unknown channel', ['--channel', 'fax', '--message', 'Hello'], /channel/],
+ ['missing push contents', ['--channel', 'push'], /message/]
+]) test(name + ' fails before a request', () => {
+ const r = run(args, false); assert.equal(r.status, 0, r.stderr)
+ assert.match(JSON.parse(r.stdout).error, pattern)
+})
+test('email preview masks credentials and shares the actual payload', () => {
+ const args = ['--channel', 'email', '--aliases', 'customer', '--email-subject', 'Hi', '--email-body', '<p>Hi</p>']
+ const p = sent(args); const r = run([...args, '--dry-run'], false)
+ assert.equal(r.status, 0, r.stderr); const d = JSON.parse(r.stdout)
+ assert.deepEqual(d.body, p.body); assert.equal(d.url, p.url)
+ assert.equal(d.headers.Authorization, '***'); assert.ok(!r.stdout.includes('dummy-secret'))
+})
diff --git a/tools/clis/onesignal.js b/tools/clis/onesignal.js
index 6977b83..57776c2 100755
--- a/tools/clis/onesignal.js
+++ b/tools/clis/onesignal.js
@@ -17,7 +17,7 @@ if ((!APP_ID) && rawArgs.length > 0) {
async function api(method, path, body) {
const headers = {
- 'Authorization': `Basic ${REST_API_KEY}`,
+ 'Authorization': `Key ${REST_API_KEY}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
@@ -69,21 +69,30 @@ async function main() {
case 'notifications':
switch (sub) {
case 'send': {
- const message = args.message
- if (!message) { result = { error: '--message required' }; break }
- const payload = {
- app_id: APP_ID,
- contents: { en: message },
- }
- if (args.heading) payload.headings = { en: args.heading }
- if (args.url) payload.url = args.url
- if (args.data) {
- try { payload.data = JSON.parse(args.data) } catch { payload.data = { value: args.data } }
+ const channel = args.channel || (args.emails ? 'email' : 'push')
+ if (!['push', 'email', 'sms'].includes(channel)) { result = { error: '--channel must be push, email, or sms' }; break }
+ if (args.emails && channel !== 'email') { result = { error: '--emails requires the email channel' }; break }
+ const payload = { app_id: APP_ID, target_channel: channel }
+ if (channel === 'email') {
+ const subject = args['email-subject'] || args.heading
+ const body = args['email-body'] || args.message
+ if (!subject) { result = { error: '--email-subject (or --heading) required for email' }; break }
+ if (!body) { result = { error: '--email-body (or --message) required for email' }; break }
+ payload.email_subject = subject
+ payload.email_body = body
+ } else {
+ if (!args.message) { result = { error: '--message required' }; break }
+ payload.contents = { en: args.message }
+ if (args.heading) payload.headings = { en: args.heading }
+ if (args.url) payload.url = args.url
+ if (args.data) {
+ try { payload.data = JSON.parse(args.data) } catch { payload.data = { value: args.data } }
+ }
}
if (args.segment) {
payload.included_segments = args.segment.split(',')
} else if (args.emails) {
- payload.include_email_tokens = args.emails.split(',')
+ payload.email_to = args.emails.split(',')
} else if (args['player-ids']) {
payload.include_player_ids = args['player-ids'].split(',')
} else if (args.aliases) {
@@ -92,13 +101,12 @@ async function main() {
} catch {
payload.include_aliases = { external_id: args.aliases.split(',') }
}
- payload.target_channel = args.channel || 'push'
} else {
payload.included_segments = ['Subscribed Users']
}
if (args['send-after']) payload.send_after = args['send-after']
- if (args.ttl) payload.ttl = Number(args.ttl)
- result = await api('POST', '/api/v1/notifications', payload)
+ if (channel !== 'email' && args.ttl) payload.ttl = Number(args.ttl)
+ result = await api('POST', `/notifications?c=${channel}`, payload)
break
}
case 'list': {
@@ -232,7 +240,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
- notifications: 'notifications [send --message <msg> --segment <s> | list | get --id <id> | cancel --id <id>]',
+ notifications: 'notifications [send --message <msg> --segment <s> | send --channel email --email-subject <s> --email-body <html> --aliases <ids> | list | get --id <id> | cancel --id <id>]',
segments: 'segments [list | create --name <n> --filters <json> | delete --id <id>]',
users: 'users [get --alias-id <id> | create --external-id <id> --email <e> | delete --alias-id <id>]',
templates: 'templates [list | get --id <id> | create --name <n> --message <msg>]',
diff --git a/tools/integrations/onesignal.md b/tools/integrations/onesignal.md
index 42a96eb..6111389 100644
--- a/tools/integrations/onesignal.md
+++ b/tools/integrations/onesignal.md
@@ -13,8 +13,8 @@ Push notification, email, SMS, and in-app messaging platform for customer engage
## Authentication
-- **Type**: REST API Key (Basic Auth)
-- **Header**: `Authorization: Basic {REST_API_KEY}`
+- **Type**: App API Key
+- **Header**: `Authorization: Key {REST_API_KEY}`
- **App ID**: Required as `app_id` in request bodies
- **Get credentials**: Dashboard > Settings > Keys & IDs
- **Security**: HTTPS required, TLS 1.2+ on port 443
@@ -27,7 +27,7 @@ Push notification, email, SMS, and in-app messaging platform for customer engage
POST https://api.onesignal.com/api/v1/notifications
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
Content-Type: application/json
{
@@ -45,7 +45,7 @@ Headers:
POST https://api.onesignal.com/api/v1/notifications
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
Content-Type: application/json
{
@@ -56,13 +56,26 @@ Headers:
}
```
+### Send email using the local CLI
+
+```bash
+node tools/clis/onesignal.js notifications send --emails "[email protected]" --email-subject "Welcome" --email-body "<p>Thanks for joining.</p>"
+node tools/clis/onesignal.js notifications send --channel email --segment "Customers" --heading "News" --message "<p>Product update.</p>"
+```
+
+Email sends require an email subject and HTML body. `--heading` and `--message` remain aliases for these email fields. `--emails` selects the email channel automatically; `--channel email` also works with existing alias, segment, or subscription targeting. Push remains the default channel, and SMS uses `--channel sms --message ...`. An explicit incompatible channel with `--emails` is rejected locally.
+
+The send command uses the current `/notifications?c=<channel>` resource, `target_channel`, and `email_to` for direct email recipients. It does not send push `contents`, `headings`, `url`, `data`, or `ttl` as email content. `--dry-run` previews the same request with masked authorization and no network request.
+
+Primary contracts: [Email](https://documentation.onesignal.com/reference/email), [SMS](https://documentation.onesignal.com/reference/sms), and [sending messages](https://documentation.onesignal.com/reference/create-message).
+
### Schedule a notification
```bash
POST https://api.onesignal.com/api/v1/notifications
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
Content-Type: application/json
{
@@ -79,7 +92,7 @@ Headers:
GET https://api.onesignal.com/api/v1/notifications?app_id={APP_ID}&limit=50&offset=0
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
### View a notification
@@ -88,7 +101,7 @@ Headers:
GET https://api.onesignal.com/api/v1/notifications/{notification_id}?app_id={APP_ID}
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
### Cancel a scheduled notification
@@ -97,7 +110,7 @@ Headers:
DELETE https://api.onesignal.com/api/v1/notifications/{notification_id}?app_id={APP_ID}
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
### List segments
@@ -106,7 +119,7 @@ Headers:
GET https://api.onesignal.com/api/v1/apps/{APP_ID}/segments
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
### Create a segment
@@ -115,7 +128,7 @@ Headers:
POST https://api.onesignal.com/api/v1/apps/{APP_ID}/segments
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
Content-Type: application/json
{
@@ -132,7 +145,7 @@ Headers:
GET https://api.onesignal.com/apps/{APP_ID}/users/by/external_id/{external_id}
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
### Create a user
@@ -141,7 +154,7 @@ Headers:
POST https://api.onesignal.com/apps/{APP_ID}/users
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
Content-Type: application/json
{
@@ -159,7 +172,7 @@ Headers:
GET https://api.onesignal.com/api/v1/templates?app_id={APP_ID}
Headers:
- Authorization: Basic {REST_API_KEY}
+ Authorization: Key {REST_API_KEY}
```
## Key Metrics