N
Novu7mo ago
sandsower

Is there a way to create a subscriber from the headless service?

We're trying to setup push notifications but apparently they need a device token. However I couldn't find a way to set this information in the headless service, it receives only a subscriber id as a parameter and nothing else related to the subscriber itself
3 Replies
Ask Inkeep
Ask Inkeep7mo ago
Hi @sandsower, I'm an AI assistant for Novu. While a team member reviews this question, I'll see if I can find relevant sources for your question. ----------------------------- While I generate an answer, here are some sources that might be helpful: * Push Channel Introduction * Pushpad * How to use Headless Notification Center * Apple Push Notification Service (APNS) ----------------------------- Answer Currently, the Novu headless notification service does not directly support creating subscribers or setting device tokens through the headless client itself. The headless client primarily initializes with an applicationIdentifier and subscriberId and is used for fetching and managing notifications (1) . To handle device tokens for push notifications, you would need to manage this on your backend. You can create a subscriber and set their device tokens using Novu's backend APIs. Here's how you could implement this: 1. Create a Subscriber: You would need to use the Novu backend API to create a subscriber or identify an existing one. This is typically done from your server. 2. Set Device Tokens: After creating a subscriber, you can use the setCredentials method from the Novu backend API to update the subscriber's profile with the device tokens required for push notifications. Here is an example using Node.js:
import { Novu } from '@novu/node';

const novu = new Novu('<NOVU_API_KEY>');

async function setupSubscriberAndToken(subscriberId, deviceTokens) {
// Identifying or creating a subscriber
await novu.subscribers.identify(subscriberId, {
firstName: 'John',
lastName: 'Doe',
});

// Setting device tokens for push notifications
await novu.subscribers.setCredentials(subscriberId, 'push-provider-id', {
deviceTokens: deviceTokens
});
}

// Usage example
setupSubscriberAndToken('subscriber-id', ['device-token-1', 'device-token-2']);
import { Novu } from '@novu/node';

const novu = new Novu('<NOVU_API_KEY>');

async function setupSubscriberAndToken(subscriberId, deviceTokens) {
// Identifying or creating a subscriber
await novu.subscribers.identify(subscriberId, {
firstName: 'John',
lastName: 'Doe',
});

// Setting device tokens for push notifications
await novu.subscribers.setCredentials(subscriberId, 'push-provider-id', {
deviceTokens: deviceTokens
});
}

// Usage example
setupSubscriberAndToken('subscriber-id', ['device-token-1', 'device-token-2']);
Ask Inkeep
Ask Inkeep7mo ago
Make sure to replace <NOVU_API_KEY>, subscriber-id, push-provider-id, and device-tokens with actual values suitable for your application. This approach allows you to manage device tokens effectively, ensuring that your push notifications are sent to the correct devices. For detailed API usage, you can refer to the Novu documentation on managing subscribers and setting credentials.
Pawan Jain
Pawan Jain7mo ago
device token can be saved using our backend sdk or using api directly Regarding the title:- Is there a way to create a subscriber from the headless service? novu creates a new subscriber if it does not exist
Want results from more Discord servers?
Add your server