You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

262 lines
6.9 KiB

/**
* Enterprise-level usage examples for the Daily Notification plugin
* Demonstrates advanced features and best practices for large-scale applications
*/
import { DailyNotification } from '@timesafari/daily-notification-plugin';
import { NotificationOptions, NotificationSettings } from '../src/definitions';
/**
* Example of implementing a notification queue system
*/
async function implementNotificationQueue() {
const plugin = new DailyNotification();
const queue: NotificationOptions[] = [];
let isProcessing = false;
async function processQueue() {
if (isProcessing || queue.length === 0) return;
isProcessing = true;
try {
const notification = queue.shift();
if (notification) {
await plugin.scheduleDailyNotification(notification);
}
} catch (error) {
console.error('Failed to process notification:', error);
// Retry logic here
} finally {
isProcessing = false;
}
}
// Add to queue
queue.push({
url: 'https://api.example.com/enterprise/updates',
time: '09:00',
title: 'Enterprise Update',
priority: 'high',
retryCount: 3,
retryInterval: 5000,
});
// Process queue
await processQueue();
}
/**
* Example of implementing A/B testing for notifications
*/
async function implementABTesting() {
const plugin = new DailyNotification();
async function scheduleABTest() {
const variants = [
{
title: 'Version A: Direct Call-to-Action',
body: 'Click here to view your daily report',
priority: 'high',
},
{
title: 'Version B: Value Proposition',
body: 'Your daily insights are ready to help you succeed',
priority: 'normal',
},
];
// Randomly select a variant
const variant = variants[Math.floor(Math.random() * variants.length)];
await plugin.scheduleDailyNotification({
url: 'https://api.example.com/ab-test-content',
time: '10:00',
...variant,
data: {
variant: variant === variants[0] ? 'A' : 'B',
timestamp: new Date().toISOString(),
},
});
}
await scheduleABTest();
}
/**
* Example of implementing notification analytics and tracking
*/
async function implementAnalyticsTracking() {
const plugin = new DailyNotification();
// Track notification delivery
plugin.on('notification', async (event) => {
await trackAnalytics({
event: 'notification_delivered',
notificationId: event.detail.id,
timestamp: new Date().toISOString(),
platform: navigator.platform,
userAgent: navigator.userAgent,
});
});
// Track notification interactions
document.addEventListener('notification_clicked', async (event) => {
await trackAnalytics({
event: 'notification_clicked',
notificationId: event.detail.id,
timestamp: new Date().toISOString(),
action: event.detail.action,
data: event.detail.data,
});
});
async function trackAnalytics(data: Record<string, any>) {
// Implement your analytics tracking logic here
console.log('Analytics Event:', data);
}
}
/**
* Example of implementing notification preferences management
*/
async function implementPreferencesManagement() {
const plugin = new DailyNotification();
interface UserPreferences {
notificationTimes: string[];
categories: string[];
priority: 'low' | 'normal' | 'high';
sound: boolean;
timezone: string;
}
async function updateUserPreferences(preferences: UserPreferences) {
// Update all scheduled notifications
for (const time of preferences.notificationTimes) {
await plugin.updateSettings({
time,
priority: preferences.priority,
sound: preferences.sound,
timezone: preferences.timezone,
});
}
// Schedule new notifications for each category
for (const category of preferences.categories) {
await plugin.scheduleDailyNotification({
url: `https://api.example.com/categories/${category}`,
time: preferences.notificationTimes[0],
title: `${category} Update`,
priority: preferences.priority,
sound: preferences.sound,
timezone: preferences.timezone,
});
}
}
// Example usage
await updateUserPreferences({
notificationTimes: ['09:00', '12:00', '18:00'],
categories: ['news', 'tasks', 'updates'],
priority: 'high',
sound: true,
timezone: 'America/New_York',
});
}
/**
* Example of implementing notification content personalization
*/
async function implementContentPersonalization() {
const plugin = new DailyNotification();
interface UserProfile {
id: string;
name: string;
preferences: {
language: string;
timezone: string;
categories: string[];
};
}
async function schedulePersonalizedNotification(profile: UserProfile) {
await plugin.scheduleDailyNotification({
url: 'https://api.example.com/personalized-content',
time: '09:00',
title: `Good morning, ${profile.name}!`,
priority: 'high',
timezone: profile.preferences.timezone,
headers: {
'X-User-ID': profile.id,
'X-Language': profile.preferences.language,
'X-Categories': profile.preferences.categories.join(','),
},
contentHandler: async (response) => {
const data = await response.json();
return {
title: data.title,
body: data.content,
data: {
...data.metadata,
userId: profile.id,
timestamp: new Date().toISOString(),
},
};
},
});
}
// Example usage
await schedulePersonalizedNotification({
id: 'user123',
name: 'John Doe',
preferences: {
language: 'en-US',
timezone: 'America/New_York',
categories: ['news', 'weather', 'tasks'],
},
});
}
/**
* Example of implementing notification rate limiting
*/
async function implementRateLimiting() {
const plugin = new DailyNotification();
class RateLimiter {
private lastNotificationTime: number = 0;
private minInterval: number = 60000; // 1 minute
async scheduleWithRateLimit(options: NotificationOptions): Promise<void> {
const now = Date.now();
if (now - this.lastNotificationTime < this.minInterval) {
throw new Error('Rate limit exceeded. Please wait before scheduling another notification.');
}
await plugin.scheduleDailyNotification(options);
this.lastNotificationTime = now;
}
}
const rateLimiter = new RateLimiter();
// Example usage
await rateLimiter.scheduleWithRateLimit({
url: 'https://api.example.com/rate-limited-content',
time: '11:00',
title: 'Rate Limited Update',
priority: 'normal',
});
}
// Export all enterprise example functions
export {
implementNotificationQueue,
implementABTesting,
implementAnalyticsTracking,
implementPreferencesManagement,
implementContentPersonalization,
implementRateLimiting,
};