Site icon Ravi Shankar

How to Schedule Notifications in an Apple Watch App Using SwiftUI

In this tutorial, we’ll guide you through creating a simple Apple Watch app using SwiftUI that schedules a local notification. This example will showcase how to notify users after a specific period, even if the app is in the background. We’ll demonstrate this by setting up a button that, when tapped, schedules a notification to appear 10 seconds later.

Overview

Local notifications are an excellent way to engage users by providing timely information or reminders. On Apple Watch, these notifications can be particularly useful for health reminders, alerts, or updates from your app. In this tutorial, we will:

  1. Set up a basic SwiftUI Apple Watch app.
  2. Request permission to send notifications.
  3. Schedule a local notification to trigger after 10 seconds.
  4. Demonstrate how the app continues to notify the user even when it’s in the background.

Prerequisites

Step 1: Setting Up the Project

Start by creating a new Apple Watch project in Xcode. Choose the “Watch App” template and select “SwiftUI” for the user interface.

Step 2: Creating the SwiftUI View

We’ll start by creating a simple user interface with a button that schedules a notification.

import SwiftUI
import UserNotifications

struct ContentView: View {
    var body: some View {
        VStack {
            Button(action: {
                sendNotification()
            }) {
                Text("Tap to Notify in 10 seconds")
            }
        }
        .onAppear {
            requestNotificationPermission()
        }
    }
    
    func requestNotificationPermission() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
            if granted {
                print("Notification permission granted.")
            } else if let error = error {
                print("Notification permission denied: \(error.localizedDescription)")
            }
        }
    }
    
    func sendNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Hello!"
        content.body = "This is a notification from your Watch app."
        content.sound = UNNotificationSound.default
        
        // Set the notification to trigger after 10 seconds
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                print("Error adding notification: \(error.localizedDescription)")
            } else {
                print("Notification scheduled for 10 seconds later.")
            }
        }
    }
}

#Preview {
    ContentView()
}

Step 3: Requesting Notification Permission

In the requestNotificationPermission() function, we ask the user for permission to send notifications. This function is called when the view appears, ensuring that we have the necessary permissions before attempting to send notifications.

Step 4: Scheduling the Notification

The sendNotification() function creates a UNMutableNotificationContent object with a title, body, and sound. It then creates a UNTimeIntervalNotificationTrigger set to trigger after 10 seconds. Finally, a UNNotificationRequestis added to the UNUserNotificationCenter.

Step 5: Running the App

To test the app:

  1. Deploy to Device: Run the app on an Apple Watch or the Watch simulator.
  2. Tap the Button: Tap the “Tap to Notify in 10 seconds” button.
  3. Send to Background: Quickly press the Digital Crown to send the app to the background.
  4. Receive Notification: After 10 seconds, you should receive a notification.

Conclusion

This tutorial demonstrated how to set up and handle local notifications in an Apple Watch app using SwiftUI. We covered requesting permissions, scheduling notifications, and handling the app’s state. This setup is a great starting point for more complex interactions and notifications in your Apple Watch applications. Whether you’re building reminders, alerts, or other interactive experiences, understanding local notifications is a crucial part of the Apple Watch development process.

Remember, local notifications are a powerful tool to keep users engaged with your app. Always ensure they are used thoughtfully and respect user preferences and permissions. Happy coding!

Exit mobile version