Example code for push notifications
The following topic provide code examples to send test messages. The code is created via the service Code Snippets.
Send a test message
If you run the script, you send a notification to all launchpads that have push notifications enabled.
try {
const r = await p9.push.send({
title: 'Hello Planet9!',
body: 'This is the main text',
})
log.info('Result after push', r);
} catch (error) {
log.error('Error: ', error);
}
Send a test message to a specific launchpad
If you run the script, you send a notification to one specific launchpad only: the demo
launchpad.
try {
const r = await p9.push.send({
title: 'Hello Planet9!',
body: 'This is the main text, for launchpad "demo"',
launchpadName: 'demo',
})
log.info('Result after push', r);
} catch (error) {
log.error('Error: ', error);
}
Send a test message to a specific user or group of users
If you run the script, you send a notification to a user or a specific group of users only: the user tbs
on the demo
launchpad.
try {
const r = await p9.push.send({
title: 'Hello Planet9!',
body: 'This is the main text, for user "tsb"',
launchpadName: 'demo',
userNames: ['tsb'],
})
log.info('Result after push', r);
} catch (error) {
log.error('Error: ', error);
}
Send a test message to a user with a specific ID
If you run the script, you send a notification to a user or a specific group of users that have a unique ID.
try {
const r = await p9.push.send({
title: 'Hello Planet9!',
body: 'This is the main text, a specific userId',
launchpadName: 'demo',
userIds: ['2e3g34e5-7352-43a6-a011-5c22fed04cc4'],
})
log.info('Result after push', r);
} catch (error) {
log.error('Error: ', error);
}