Skip to content

Complete iOS push notification demo with SwiftUI frontend, Vapor backend, rich media attachments, and notification service extension. Features device token management, APNs integration, and Docker deployment.

License

Notifications You must be signed in to change notification settings

banghuazhao/ios-push-notification-full-stack-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iOS Push Notification Full-Stack Demo

A complete demonstration of iOS push notifications with a SwiftUI frontend and Vapor backend server. This project showcases how to implement push notifications with image attachments, device token management, and a notification service extension for rich content delivery.

example screenshot

🏗️ Project Structure

ios-push-notification-full-stack-demo/ ├── App/ # iOS Application │ ├── PushNotifications/ # Main iOS app │ └── AttachmentNotification/ # Notification Service Extension └── Backend/ # Vapor Web Server ├── Sources/WebService/ # Server source code ├── AuthKey_XXXX.p8 # Your APNs Authentication Key └── docker-compose.yml # Docker configuration 

🚀 Features

  • iOS Push Notifications: Complete implementation with device token registration
  • Rich Media Notifications: Support for image attachments in notifications
  • Notification Service Extension: Processes and enhances notifications with media
  • Token Management: RESTful API for device token storage and management
  • APNs Integration: Server-side push notification delivery using APNs
  • SwiftUI Frontend: Modern iOS app interface
  • Vapor Backend: Swift server with PostgreSQL database

📋 Prerequisites

iOS Development

  • Xcode 15.0 or later
  • iOS 17.0 or later
  • Apple Developer Account (for push notifications)
  • Physical iOS device (push notifications don't work in simulator)

Backend Development

  • Swift 6.0 or later
  • Docker and Docker Compose
  • PostgreSQL (via Docker)

🛠️ Setup Instructions

1. iOS App Configuration

Step 1: Configure App ID and Push Notifications

  1. Go to Apple Developer Console
  2. Create or select your App ID
  3. Enable "Push Notifications" capability
  4. Create an APNs Authentication Key:
    • Go to "Keys" section
    • Create a new key with "Apple Push Notifications service (APNs)" enabled
    • Download the .p8 file and note the Key ID and Team ID

Step 2: Update Backend Configuration

  1. Add your APNs authentication key file AuthKey_XXXX.p8 to Backend folder
  2. Update the backend configuration with your credentials (see Backend Setup section)

Step 3: Update iOS App Server URL

  1. Open App/PushNotifications/AppDelegate.swift
  2. Update the server URL on line 47:
    PushNotifications.send(token: deviceToken, to: "http://YOUR_SERVER_IP:8080/token")
    Replace YOUR_SERVER_IP with your actual server IP address. You can find your localhost IP attach from macOS network settings

2. Backend Setup

  1. Navigate to Backend directory:

    cd Backend
  2. Update APNs Configuration:

    • Add your APNs authentication key file AuthKey_XXXX.p8 to Backend folder
    • Update the configuration in Sources/WebService/configure.swift with your:
      • Key ID
      • Team ID
      • Bundle ID
    • Update the name of your authentication key file AuthKey_XXXX.p8 in Dockerfile
  3. Build and start the services: Login and run docker in your Mac. Then run

    docker compose up db docker compose up app
  4. Verify the server is running:

    • Visit http://localhost:8080 in your browser
    • You should see "It works!"

3. iOS App Setup

  1. Open the project:

    open App/PushNotifications.xcodeproj
  2. Configure your development team:

    • Select the project in Xcode
    • Go to "Signing & Capabilities"
    • Select your development team
    • Ensure "Push Notifications" capability is enabled
  3. Build and run on a physical device:

    • Connect your iOS device
    • Select your device as the target
    • Build and run the app (⌘+R)

🧪 Testing Push Notifications

Step 1: Register Device Token

  1. Launch the iOS app on your physical device
  2. Grant notification permissions when prompted
  3. The app will automatically register for push notifications and send the device token to your server
  4. Check the server logs to confirm token registration

Step 2: Send a Test Notification

curl -X POST http://localhost:8080/token/notify

Step 3: Verify Notification Delivery

  1. Put the iOS app in the background or lock the device
  2. Send the notification using the API call above
  3. You should receive a push notification with:
    • Title: "Hello!"
    • Body: "How are you today?"
    • Image attachment (downloaded and displayed by the notification service extension)

🔧 API Endpoints

The backend provides the following RESTful endpoints:

Method Endpoint Description
GET / Health check - returns "It works!"
GET /hello Test endpoint - returns "Hello, world!"
POST /token Register a new device token
DELETE /token/:token Remove a device token
POST /token/notify Send push notification to all registered devices

Example API Usage

Register a device token:

curl -X POST http://localhost:8080/token \ -H "Content-Type: application/json" \ -d '{"token": "your_device_token_here", "debug": true}'

Send notification to all devices:

curl -X POST http://localhost:8080/token/notify

🏗️ Architecture

iOS App Architecture

  • MVVM Pattern: Uses SwiftUI with Observable view models
  • Combine Framework: Handles reactive programming and data flow
  • Notification Center: Manages push notification registration and handling
  • Service Extension: Processes rich media notifications

Backend Architecture

  • Vapor Framework: Swift web framework for the server
  • Fluent ORM: Database abstraction layer
  • PostgreSQL: Persistent storage for device tokens
  • APNs Library: Apple Push Notification service integration

🐛 Troubleshooting

Common Issues

  1. No notifications received:

    • Ensure you're testing on a physical device (not simulator)
    • Ensure your device is not in Do Not Disturb mode
    • Check that push notifications are enabled in device settings
    • Verify your APNs authentication key is correctly configured
    • Check server logs for any errors
  2. Token registration fails:

    • Verify the server URL in AppDelegate.swift is correct
    • Ensure the backend server is running and accessible
    • Check network connectivity between device and server
  3. Docker issues:

    • Ensure Docker is running
    • Try docker compose down -v to reset volumes
    • Check docker compose logs for error messages
  4. Build errors:

    • Clean build folder (⌘+Shift+K)
    • Update dependencies: swift package resolve
    • Ensure you're using the correct Swift version

📝 Development Notes

  • The project uses Swift 6.0 with modern concurrency features
  • Push notifications require a valid Apple Developer account
  • The notification service extension handles image downloads and attachment creation
  • Device tokens are automatically cleaned up when they become invalid
  • The backend supports both development and production APNs environments

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly on a physical device
  5. Submit a pull request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

About

Complete iOS push notification demo with SwiftUI frontend, Vapor backend, rich media attachments, and notification service extension. Features device token management, APNs integration, and Docker deployment.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published