# Cron Jobs for Birthday Reminder PWA

This directory contains cron job scripts for automated tasks.

## Daily Notification Cron Job

**File:** `send_daily_notifications.php`

**Purpose:** Sends push notifications to all users at 9:00 AM IST daily with their birthdays and reminders for the day.

### Setup Instructions

#### Linux/Unix/Mac

Add the following line to your crontab:

```bash
# Edit crontab
crontab -e

# Add this line (adjust path to your installation)
0 9 * * * /usr/bin/php /path/to/birthday_reminder/cron/send_daily_notifications.php >> /path/to/birthday_reminder/logs/cron.log 2>&1
```

#### Windows Task Scheduler

1. Open Task Scheduler
2. Create a new Basic Task
3. Name: "Birthday Reminder Daily Notifications"
4. Trigger: Daily at 9:00 AM
5. Action: Start a program
   - Program: `C:\path\to\php.exe`
   - Arguments: `C:\path\to\birthday_reminder\cron\send_daily_notifications.php`
   - Start in: `C:\path\to\birthday_reminder\cron`

#### Laragon (Windows)

If using Laragon, you can use the built-in PHP:

1. Open Task Scheduler
2. Create a new Basic Task
3. Name: "Birthday Reminder Daily Notifications"
4. Trigger: Daily at 9:00 AM
5. Action: Start a program
   - Program: `C:\laragon\bin\php\php-8.x.x\php.exe` (adjust version)
   - Arguments: `C:\laragon\www\birthday_reminder\cron\send_daily_notifications.php`
   - Start in: `C:\laragon\www\birthday_reminder\cron`

### Manual Testing

You can manually run the cron job to test it:

```bash
# Linux/Unix/Mac
php cron/send_daily_notifications.php

# Windows
php.exe cron\send_daily_notifications.php
```

## Test Notification Script

**File:** `test_notifications.php`

**Purpose:** Send test notifications to a specific user for debugging.

### Usage

```bash
# List all users
php cron/test_notifications.php

# Send test notification to user ID 1
php cron/test_notifications.php 1
```

This script will:
1. Check if the user exists
2. Check if the user has active push subscriptions
3. Generate a notification payload based on today's reminders
4. Send the notification to all user's subscriptions
5. Display detailed results

## Troubleshooting

### No notifications received

1. **Check VAPID keys are configured:**
   ```bash
   php config/generate_vapid_keys.php
   ```
   Add the generated keys to `config/config.php`

2. **Check user has subscribed:**
   - Open the PWA in browser
   - Click "Enable Notifications" when prompted
   - Grant notification permission

3. **Check cron job is running:**
   - Check logs: `logs/error.log` and `logs/cron.log`
   - Run manually: `php cron/send_daily_notifications.php`

4. **Check service worker is registered:**
   - Open browser DevTools
   - Go to Application > Service Workers
   - Verify service worker is active

5. **Test with test script:**
   ```bash
   php cron/test_notifications.php [user_id]
   ```

### Notifications not sending at 9 AM

1. **Verify cron job is scheduled:**
   ```bash
   # Linux/Unix/Mac
   crontab -l
   
   # Windows
   # Check Task Scheduler
   ```

2. **Check timezone:**
   - Ensure server timezone is set to IST (Asia/Kolkata)
   - Check `date_default_timezone_set('Asia/Kolkata')` in config

3. **Check cron logs:**
   ```bash
   tail -f logs/cron.log
   tail -f logs/error.log
   ```

### Web Push library errors

1. **Install dependencies:**
   ```bash
   composer require minishlink/web-push
   ```

2. **Check PHP version:**
   - Requires PHP 7.4 or higher
   - Check: `php -v`

3. **Check required extensions:**
   ```bash
   php -m | grep -E 'curl|openssl|mbstring'
   ```

## Logs

All cron job activity is logged to:
- `logs/error.log` - General application logs
- `logs/cron.log` - Cron job specific output (if redirected)

View logs:
```bash
# View last 50 lines
tail -n 50 logs/error.log

# Follow logs in real-time
tail -f logs/error.log
```

## Security Notes

1. **Protect cron directory:**
   - Add `.htaccess` to prevent web access
   - Only allow CLI execution

2. **Secure VAPID keys:**
   - Never commit keys to version control
   - Use environment variables in production
   - Regenerate keys if compromised

3. **Monitor logs:**
   - Regularly check for errors
   - Set up log rotation
   - Alert on failures

## Additional Cron Jobs

You can add more cron jobs as needed:

### Midnight Data Refresh (Optional)

```bash
# Run at midnight IST to clean up old data
0 0 * * * /usr/bin/php /path/to/birthday_reminder/cron/cleanup_old_data.php
```

### Weekly Backup (Optional)

```bash
# Run weekly backup on Sunday at 2 AM
0 2 * * 0 /usr/bin/php /path/to/birthday_reminder/cron/backup_database.php
```
