I'm experiencing this weird problem where during development the following works flawlessly but is very flaky in production.
I'm dynamically adding actions to notifications from my NotificationContent extension.
When the user clicks on an action userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) should be called from where my app fires off an HTTP request based on the action that was selected.
As already mentioned this works without issues during development but is extremely flaky in production.
UNUserNotificationCenterDelegate:
extension RealPushNotificationsHandler: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let actionPayload = userInfo["action"] as? String,
let notificationUUID = userInfo["uuid"] as? String {
// handleAction fires off an HTTP request
handleAction(notificationUUID: notificationUUID, actionIdentifier: response.actionIdentifier, actionPayload: actionPayload, completionHandler: completionHandler)
}
}
NotificationContent/NotificationViewController.swift:
class NotificationViewController: UIViewController, UNNotificationContentExtension {
override func viewDidLoad() {
super.viewDidLoad()
}
func didReceive(_ notification: UNNotification) {
var actions: [UNNotificationAction] = []
// Parse actions from notification.request.content.userInfo
[...]
// Append actions to notification
extensionContext?.notificationActions = actions
}
}