extension AppDelegate {
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
if let userInfo = userInfo as? [String: Any] {
d1Task.processNotification(userInfo) { response, error in
guard error == nil else {
// Refer to D1 SDK Integration – Error Management section.
completionHandler(.noData)
return
}
guard let response = response, let type = response[PushResponse.Key.messageType] else {
completionHandler(.noData)
return
}
guard type != PushResponse.typeUnknown else {
// The notification is not recognized by the SDK.
completionHandler(.noData)
return
}
guard type == PushResponse.typeMessaging,
let id = response[PushResponse.Key.messageID],
let message = Message.message(withID: id) else {
completionHandler(.noData)
return
}
let content = UNMutableNotificationContent()
// Constructs the notification based on message
content.title = message.title ?? "Notification"
content.body = message.message ?? ""
content.targetContentIdentifier = message.action.
// Shows the notification to the end user.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in }
completionHandler(.newData)
}
} else {
completionHandler(.noData)
}
}
}