mattermost-mobile/ios/NotificationService/NotificationService.swift
Elias Nahum 5bf96272ae
Notification receipt delivery for Android and iOS (#2755)
* Notification receipt delivery for Android and iOS

* Update Fastlane with iOS notification service extension

* User JSONObject to build response
2019-05-15 12:45:26 -04:00

42 lines
1.4 KiB
Swift

import UserNotifications
import UploadAttachments
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
UploadSession.shared.notificationReceipt(
notificationId: bestAttemptContent.userInfo["ack_id"],
receivedAt: Date().millisencondsSince1970,
type: bestAttemptContent.userInfo["type"]
)
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension Date {
var millisencondsSince1970: Int {
return Int((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds: Int) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}