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.
165 lines
4.1 KiB
165 lines
4.1 KiB
/**
|
|
* Example usage of the Daily Notification plugin
|
|
* Demonstrates various features and best practices
|
|
*/
|
|
|
|
import { registerPlugin } from '@capacitor/core';
|
|
import { DailyNotificationPlugin } from '../src/definitions';
|
|
|
|
const DailyNotification = registerPlugin<DailyNotificationPlugin>('DailyNotification');
|
|
|
|
/**
|
|
* Basic setup for daily notifications
|
|
*/
|
|
async function setupBasicNotifications() {
|
|
try {
|
|
await DailyNotification.scheduleDailyNotification({
|
|
url: 'https://api.example.com/daily-content',
|
|
time: '08:00', // 8 AM
|
|
title: 'Daily Update',
|
|
body: 'Your daily content is ready!'
|
|
});
|
|
console.log('Daily notifications scheduled successfully');
|
|
} catch (error) {
|
|
console.error('Failed to schedule notifications:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Advanced setup with custom options
|
|
*/
|
|
async function setupAdvancedNotifications() {
|
|
try {
|
|
await DailyNotification.scheduleDailyNotification({
|
|
url: 'https://api.example.com/daily-content',
|
|
time: '09:00', // 9 AM
|
|
title: 'Daily Digest',
|
|
body: 'Your personalized daily digest is ready',
|
|
sound: true,
|
|
vibrate: true,
|
|
priority: 'high',
|
|
retryCount: 3,
|
|
retryInterval: 15, // minutes
|
|
cacheDuration: 24, // hours
|
|
headers: {
|
|
'Authorization': 'Bearer your-token'
|
|
}
|
|
});
|
|
console.log('Advanced notification setup completed');
|
|
} catch (error) {
|
|
console.error('Failed to setup advanced notifications:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle notification response
|
|
*/
|
|
async function handleNotificationResponse() {
|
|
try {
|
|
const response = await DailyNotification.getLastNotification();
|
|
if (response) {
|
|
console.log('Last notification:', response);
|
|
// Handle the notification data
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to get last notification:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cancel all scheduled notifications
|
|
*/
|
|
async function cancelNotifications() {
|
|
try {
|
|
await DailyNotification.cancelAllNotifications();
|
|
console.log('All notifications cancelled');
|
|
} catch (error) {
|
|
console.error('Failed to cancel notifications:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check notification status
|
|
*/
|
|
async function checkNotificationStatus() {
|
|
try {
|
|
const status = await DailyNotification.getNotificationStatus();
|
|
console.log('Notification status:', status);
|
|
return status;
|
|
} catch (error) {
|
|
console.error('Failed to get notification status:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update notification settings
|
|
*/
|
|
async function updateNotificationSettings() {
|
|
try {
|
|
await DailyNotification.updateSettings({
|
|
time: '10:00', // Change to 10 AM
|
|
sound: false, // Disable sound
|
|
priority: 'normal'
|
|
});
|
|
console.log('Notification settings updated');
|
|
} catch (error) {
|
|
console.error('Failed to update settings:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Example of handling network errors
|
|
*/
|
|
async function handleNetworkErrors() {
|
|
try {
|
|
await DailyNotification.scheduleDailyNotification({
|
|
url: 'https://api.example.com/daily-content',
|
|
time: '08:00',
|
|
retryCount: 3,
|
|
retryInterval: 15,
|
|
offlineFallback: true
|
|
});
|
|
} catch (error) {
|
|
if (error.code === 'NETWORK_ERROR') {
|
|
console.log('Network error, will retry later');
|
|
} else {
|
|
console.error('Unexpected error:', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Example of using custom content handlers
|
|
*/
|
|
async function useCustomContentHandler() {
|
|
try {
|
|
await DailyNotification.scheduleDailyNotification({
|
|
url: 'https://api.example.com/daily-content',
|
|
time: '08:00',
|
|
contentHandler: async (response) => {
|
|
// Custom processing of the API response
|
|
const data = await response.json();
|
|
return {
|
|
title: data.title,
|
|
body: data.summary,
|
|
data: data.details
|
|
};
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to setup custom content handler:', error);
|
|
}
|
|
}
|
|
|
|
// Export all example functions
|
|
export {
|
|
setupBasicNotifications,
|
|
setupAdvancedNotifications,
|
|
handleNotificationResponse,
|
|
cancelNotifications,
|
|
checkNotificationStatus,
|
|
updateNotificationSettings,
|
|
handleNetworkErrors,
|
|
useCustomContentHandler
|
|
};
|