mattermost-mobile/ios/NotificationService/NotificationService.swift
Miguel Alatzar ba76e5eac7 [MM-16232] ID loaded push notifications (#3562)
* [MM-16232] Android: Fetch notification in notificationReceiptDelivery (#3552)

* Fetch notification in notificationReceiptDelivery

* Fix patch

* Fix patch take 2

* No need to send user_id to ack endpoint

* Just putString in mNotificationProps

* Fix patch take 3

* Revert react-native-notifications patch

* Update patch and fix rejections

* Remove trailing newline in patch

* Move PushNotification changes to end of patch

* npm cache test

* Revert "npm cache test"

This reverts commit d31030aaeeb010c1c3d22a5f6196191eeb849add.

* Created patch after upgrading node

* Created patch after upgrading node take 2

* Remove androidx changes from patch

* Patch packages then jetify

* Cache node_modules without patches

* Remove adding of default message (#3557)

* [MM-16232] iOS: Fetch id-loaded push notification from server (#3556)

* Fetch notification from server

* Parse fetched notification response

* Fix id-loaded notifications for DM/GM's

* audit fix

* Only add keys if they exist

* Throw exception if response code is not 200
2019-11-18 19:29:49 -03:00

64 lines
2.7 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 {
let ackId = bestAttemptContent.userInfo["ack_id"]
let type = bestAttemptContent.userInfo["type"]
let postId = bestAttemptContent.userInfo["post_id"]
UploadSession.shared.notificationReceipt(
notificationId: ackId,
receivedAt: Date().millisencondsSince1970,
type: type,
postId: postId
) { data, error in
if (type as? String == "id_loaded") {
guard let data = data, error == nil else {
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as! Dictionary<String,Any>
bestAttemptContent.title = json!["channel_name"] as! String
bestAttemptContent.body = json!["message"] as! String
bestAttemptContent.userInfo["channel_name"] = json!["channel_name"] as! String
bestAttemptContent.userInfo["team_id"] = json!["team_id"] as? String
bestAttemptContent.userInfo["sender_id"] = json!["sender_id"] as! String
bestAttemptContent.userInfo["sender_name"] = json!["sender_name"] as! String
bestAttemptContent.userInfo["root_id"] = json!["root_id"] as? String
bestAttemptContent.userInfo["override_username"] = json!["override_username"] as? String
bestAttemptContent.userInfo["override_icon_url"] = json!["override_icon_url"] as? String
bestAttemptContent.userInfo["from_webhook"] = json!["from_webhook"] as? String
}
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)
}
}