Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

10.7 KB
Raw Blame
import { HogFunctionTemplate } from '~/cdp/types'

export const template: HogFunctionTemplate = {
    free: false,
    status: 'hidden',
    type: 'destination',
    id: 'template-native-push',
    name: 'Push notification',
    description: 'Send push notifications to mobile devices via FCM or APNS',
    icon_url: '/static/posthog-icon.svg',
    category: ['Communication'],
    code_language: 'hog',
    code: `
let delivery := inputs.delivery ?? 'notification'
let payload := {}
if (delivery != 'notification') payload.delivery := delivery

if (delivery != 'silent') {
    payload.title := inputs.title ?? ''
    payload.body := inputs.body
    if (inputs.image) payload.image := inputs.image
}
// The link and group travel as data so an app that draws its own notifications can read them too.
let data := inputs.data ?? {}
if (inputs.link) data.link := inputs.link
if (inputs.group) data.group := inputs.group
if (length(keys(data)) > 0) payload.data := data
if (inputs.collapseKey) payload.collapseKey := inputs.collapseKey
if (inputs.ttlSeconds) payload.ttlSeconds := inputs.ttlSeconds

// Android-specific overrides
let android := {}
if (inputs.android_priority) android.priority := inputs.android_priority
if (inputs.android_channelId) android.channelId := inputs.android_channelId
if (inputs.android_sound) android.sound := inputs.android_sound
if (inputs.android_tag) android.tag := inputs.android_tag
if (inputs.android_icon) android.icon := inputs.android_icon
if (inputs.android_color) android.color := inputs.android_color
if (inputs.android_clickAction) android.clickAction := inputs.android_clickAction
if (length(keys(android)) > 0) payload.android := android

// iOS (APNS) overrides
let apns := {}
if (inputs.ios_sound) apns.sound := inputs.ios_sound
if (inputs.ios_badge != null) apns.badge := inputs.ios_badge
if (inputs.ios_category) apns.category := inputs.ios_category
if (inputs.ios_threadId) {
    apns.threadId := inputs.ios_threadId
} else if (inputs.group) {
    apns.threadId := inputs.group
}
if (inputs.ios_interruptionLevel) apns.interruptionLevel := inputs.ios_interruptionLevel
if (inputs.ios_subtitle) apns.subtitle := inputs.ios_subtitle
if (inputs.ios_mutableContent) apns.mutableContent := inputs.ios_mutableContent
if (length(keys(apns)) > 0) payload.apns := apns

if (not inputs.channels or length(inputs.channels) == 0) {
    throw Error('No push channel configured. Select at least one channel.')
}

// Fan out to every selected channel inside the async function: a hog function only runs one async
// call per invocation, so pass all channel integration ids in a single call. The recipient only has a
// device token for the platform they registered, so other channels record push_skipped.
let res := sendPushNotification({
    'integrationIds': arrayMap(channel -> channel.$integration_id, inputs.channels),
    'distinctId': inputs.distinctId,
    'payload': payload
})
if (not res.success) {
    throw Error(f'Failed to send push notification: {res.error}')
}
`,
    inputs_schema: [
        {
            key: 'distinctId',
            type: 'string',
            label: 'Distinct ID',
            secret: false,
            required: true,
            description: 'Distinct ID of the person to send the notification to.',
            default: '{event.distinct_id}',
        },
        {
            key: 'channels',
            type: 'integration_multi',
            integration: 'firebase,apns',
            label: 'Push channels',
            secret: false,
            required: true,
            description:
                'Which channels to send through. Each recipient gets the notification on the device they registered.',
        },
        {
            key: 'delivery',
            type: 'choice',
            label: 'Delivery',
            choices: [
                { value: 'notification', label: 'Show a notification' },
                { value: 'android_data_only', label: 'Data only on Android, notification on iOS' },
                { value: 'silent', label: 'Silent, show nothing' },
            ],
            secret: false,
            required: false,
            description:
                "Data only and silent pushes go to your app's code instead of the system tray. Put what the app needs in the custom data.",
            default: 'notification',
        },
        {
            key: 'title',
            type: 'string',
            label: 'Notification title',
            secret: false,
            required: false,
            description: 'The title of the push notification. Not used when the push is silent.',
            default: 'Notification from {event.event}',
        },
        {
            key: 'body',
            type: 'string',
            label: 'Notification body',
            secret: false,
            required: false,
            description: 'The body text of the push notification.',
            default: '',
            templating: 'liquid',
        },
        {
            key: 'image',
            type: 'string',
            label: 'Image URL',
            secret: false,
            required: false,
            description: 'URL of an image to display in the notification.',
        },
        {
            key: 'link',
            type: 'string',
            label: 'Link',
            secret: false,
            required: false,
            description:
                'Where a tap should take the person, such as myapp://chat/123 or a web URL. Sent to the app as the "link" key in the custom data, for the app to open.',
        },
        {
            key: 'group',
            type: 'string',
            label: 'Group',
            secret: false,
            required: false,
            description:
                'Notifications with the same group are bundled together, such as one conversation. Used as the iOS thread ID, and sent to the app as the "group" key in the custom data for Android.',
        },
        {
            key: 'data',
            type: 'json',
            label: 'Custom data payload',
            secret: false,
            required: false,
            description: 'Custom key-value data delivered to the app (values must be strings for FCM).',
            default: {},
        },
        {
            key: 'collapseKey',
            type: 'string',
            label: 'Collapse key',
            secret: false,
            required: false,
            description:
                'Identifier for grouping notifications. Only the latest notification with a given key is shown.',
        },
        {
            key: 'ttlSeconds',
            type: 'number',
            label: 'TTL (seconds)',
            secret: false,
            required: false,
            description: 'Time-to-live in seconds. How long FCM stores the message if the device is offline.',
        },
        // Android-specific options (used when delivering via Firebase)
        {
            key: 'android_priority',
            type: 'choice',
            label: 'Android priority',
            choices: [
                { value: 'normal', label: 'Normal' },
                { value: 'high', label: 'High' },
            ],
            description: 'Message delivery priority. High priority wakes the device immediately.',
            required: false,
        },
        {
            key: 'android_channelId',
            type: 'string',
            label: 'Android channel ID',
            description: 'Android notification channel ID (Android 8.0+).',
            required: false,
        },
        {
            key: 'android_sound',
            type: 'string',
            label: 'Android sound',
            description: 'Sound to play on Android. Use "default" for the system default.',
            required: false,
        },
        {
            key: 'android_tag',
            type: 'string',
            label: 'Android tag',
            description: 'Notification tag. Replaces an existing notification with the same tag.',
            required: false,
        },
        {
            key: 'android_icon',
            type: 'string',
            label: 'Android icon',
            description: 'Notification icon resource name.',
            required: false,
        },
        {
            key: 'android_color',
            type: 'string',
            label: 'Android color',
            description: 'Notification icon color in #RRGGBB format.',
            required: false,
        },
        {
            key: 'android_clickAction',
            type: 'string',
            label: 'Android click action',
            description: 'Activity to launch when the notification is tapped.',
            required: false,
        },
        // iOS-specific options (used when delivering via Apple Push, or to iOS devices via Firebase)
        {
            key: 'ios_sound',
            type: 'string',
            label: 'iOS sound',
            description: 'Sound file name or "default" for the system default.',
            required: false,
        },
        {
            key: 'ios_badge',
            type: 'number',
            label: 'iOS badge count',
            description: 'App icon badge number.',
            required: false,
        },
        {
            key: 'ios_subtitle',
            type: 'string',
            label: 'iOS subtitle',
            description: 'Additional text below the title.',
            required: false,
        },
        {
            key: 'ios_category',
            type: 'string',
            label: 'iOS category',
            description: 'Notification category for actionable notifications.',
            required: false,
        },
        {
            key: 'ios_threadId',
            type: 'string',
            label: 'iOS thread ID',
            description: 'Identifier for grouping notifications in the notification center.',
            required: false,
        },
        {
            key: 'ios_interruptionLevel',
            type: 'choice',
            label: 'iOS interruption level',
            choices: [
                { value: 'passive', label: 'Passive' },
                { value: 'active', label: 'Active' },
                { value: 'time-sensitive', label: 'Time sensitive' },
                { value: 'critical', label: 'Critical' },
            ],
            description: 'How prominently the notification is presented (iOS 15+).',
            required: false,
        },
        {
            key: 'ios_mutableContent',
            type: 'boolean',
            label: 'iOS mutable content',
            description: 'Allow a notification service extension to modify the content before display.',
            required: false,
            default: false,
        },
    ],
    // Providers are now top-level inputs (fcm_provider / apns_provider), so this step renders as a
    // flat form like email — no mappings UI. Explicitly empty to clear any previously-synced mappings.
    mapping_templates: [],
}