Leveraging Broadcast Push Notifications for Live Activities in iOS 18

 

Apple has introduced an innovative way to broadcast updates to a large audience with a single push notification in iOS 18. This new capability, known as Broadcast Push Notifications, allows apps to send updates to multiple devices at once, ensuring Live Activities remain up-to-date efficiently. In this blog, we will explore how to use this feature with ActivityKit and Apple Push Notification Service (APNs), complete with code examples.

What Are Live Activities?

Live Activities allow users to track ongoing events directly from the Lock Screen or Dynamic Island. They are commonly used for tracking deliveries, live sports scores, or flight updates. Traditionally, updating a Live Activity required sending unique push notifications for each device, which could be inefficient when reaching a large audience.

Introducing Broadcast Push Notifications

With iOS 18, developers can now use Broadcast Push Notifications to send a single update to all users subscribed to a particular channel. A channel is a unique identifier for a shared event, such as a sports game or flight status.

How It Works:

  1. Create a Channel — A unique channel ID is generated.
  2. Subscribe to the Channel — Live Activities listen for updates via the channel.
  3. Send Broadcast Push Notification — A single notification updates all subscribers.

Step-by-Step Implementation

1. Enabling Broadcast Push Notifications

Before integrating broadcast updates, enable the capability in the Apple Developer Portal:

  1. Navigate to Certificates, Identifiers & Profiles.
  2. Select your app ID and enable Broadcast Push Notifications under Push Notifications.

2. Creating a Channel

You can create a channel using Push Notifications Console or via API.

Creating a Channel via Push Notifications Console:

  1. Open the Push Notifications Console.
  2. Navigate to Channels and select New Channel.
  3. Choose an environment (Development or Production).
  4. Select a Message Storage Policy:
  • No Storage: Updates are only delivered to online devices.
  • Most Recent Message: Stores the latest update for new subscribers.

5. Click Create Channel to generate a channel ID.

3. Subscribing to a Channel in Your App

Once a channel is created, your app must subscribe to it before starting a Live Activity.

Requesting a Channel ID from the Server

func fetchChannelID(for event: String, completion: @escaping (String?) -> Void) {
let url = URL(string: "https://yourserver.com/getChannelID?event=\(event)")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, let channelID = String(data: data, encoding: .utf8) else {
completion(nil)
return
}
completion(channelID)
}.resume()
}

Starting a Live Activity with a Channel ID

import ActivityKit

struct SportsActivityAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
var homeTeamScore: Int
var awayTeamScore: Int
}
var eventName: String
}
func startLiveActivity(channelID: String) {
let attributes = SportsActivityAttributes(eventName: "Soccer Match")
let initialState = SportsActivityAttributes.ContentState(homeTeamScore: 0, awayTeamScore: 0)

let activityContent = ActivityContent(state: initialState, staleDate: nil)

do {
let activity = try Activity<SportsActivityAttributes>.request(attributes: attributes, content: activityContent, pushType: .channel(channelID))
print("Live Activity started with channel ID: \(channelID)")
} catch {
print("Error starting Live Activity: \(error)")
}
}

4. Sending a Broadcast Push Notification

Broadcast push notifications must be sent through APNs using your backend server.

APNs Payload Example:

{
"aps": {
"timestamp": 1710500000,
"alert": "Home Team leads 2-1!",
"mutable-content": 1
},
"channel": "base64_encoded_channel_id"
}

Sending Notification via cURL:

curl -v -d '{
"aps": {"alert": "Goal! Home Team scores!", "mutable-content": 1},
"channel": "your_channel_id"
}' \
-H "apns-topic: com.yourapp.push-type.liveactivity" \
-H "authorization: bearer YOUR_AUTH_TOKEN" \
--http2 https://api.push.apple.com/3/device/YOUR_DEVICE_TOKEN

5. Managing Channels

The total number of channels is limited, so removing inactive ones is recommended.

Deleting a Channel via API:

curl -X DELETE "https://api.push.apple.com/3/channel/YOUR_CHANNEL_ID" \
-H "authorization: bearer YOUR_AUTH_TOKEN"

Conclusion

Broadcast Push Notifications significantly improve efficiency when updating Live Activities for large audiences. By leveraging APNs channels, developers can now:

  • Reduce the overhead of storing individual push tokens.
  • Send a single notification to multiple devices.
  • Improve the user experience by ensuring timely updates.

Start experimenting with Broadcast Push Notifications today and enhance your app’s Live Activity experience!

🔗 Additional Resources:

🚀 Happy coding!

Comments

Popular posts from this blog

Dependency Injection in iOS with SwiftUI

Using Core ML with SwiftUI: Build an AI-Powered App

CI/CD for iOS Projects with Xcode: A Complete Guide