Push Notification Expert
Instructions
When working with notifications:
- •Understand the notification flow (APNs -> App -> User)
- •Properly configure notification categories and actions
- •Handle both foreground and background delivery
- •Test notification handling thoroughly
watchOS Notification Patterns
Permission Request
swift
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .sound, .badge]
) { granted, error in
if granted {
// Register for remote notifications
WKExtension.shared().registerForRemoteNotifications()
}
}
Notification Categories
swift
// Define actions
let approveAction = UNNotificationAction(
identifier: "APPROVE",
title: "Approve",
options: [.foreground]
)
let rejectAction = UNNotificationAction(
identifier: "REJECT",
title: "Reject",
options: [.destructive]
)
// Create category
let category = UNNotificationCategory(
identifier: "ACTION_CATEGORY",
actions: [approveAction, rejectAction],
intentIdentifiers: [],
options: [.customDismissAction]
)
// Register
UNUserNotificationCenter.current().setNotificationCategories([category])
Handling Responses
swift
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
switch response.actionIdentifier {
case "APPROVE":
// Handle approval
case "REJECT":
// Handle rejection
case UNNotificationDefaultActionIdentifier:
// User tapped notification body
case UNNotificationDismissActionIdentifier:
// User dismissed notification
default:
break
}
completionHandler()
}
}
Local Notifications
swift
let content = UNMutableNotificationContent() content.title = "Action Required" content.body = "Claude needs your approval" content.categoryIdentifier = "ACTION_CATEGORY" content.userInfo = ["action_id": "abc123"] let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request)
Best Practices
- •Always handle notification permissions gracefully
- •Use actionable notifications for quick responses
- •Include relevant data in
userInfodictionary - •Test with both app foreground and background
- •Handle notification dismissal appropriately