From 3199e25af1d5377daa856257eb5138745f4bd720 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Thu, 25 Jun 2020 21:12:53 +0200 Subject: [PATCH] Automated cherry pick of #4433 (#4483) * Don't retry nor use response message on 401 * Explicitly call contentHandler on retry exhaustion * Handle all non 200 responses for id_loaded notifications Co-authored-by: Miguel Alatzar Co-authored-by: Elias Nahum --- .../mattermost/rnbeta/ReceiptDelivery.java | 19 +++++++++++++++++++ .../NotificationService.swift | 8 +++++++- .../UploadAttachments/UploadSession.swift | 8 ++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/java/com/mattermost/rnbeta/ReceiptDelivery.java b/android/app/src/main/java/com/mattermost/rnbeta/ReceiptDelivery.java index 3a80de4b1..0c49b76aa 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/ReceiptDelivery.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/ReceiptDelivery.java @@ -106,8 +106,27 @@ public class ReceiptDelivery { Response response = client.newCall(request).execute(); String responseBody = response.body().string(); if (response.code() != 200) { + switch (response.code()) { + case 302: + promise.reject("Receipt delivery failure", "StatusFound"); + return; + case 400: + promise.reject("Receipt delivery failure", "StatusBadRequest"); + return; + case 401: + promise.reject("Receipt delivery failure", "Unauthorized"); + return; + case 500: + promise.reject("Receipt delivery failure", "StatusInternalServerError"); + return; + case 501: + promise.reject("Receipt delivery failure", "StatusNotImplemented"); + return; + } + throw new Exception(responseBody); } + JSONObject jsonResponse = new JSONObject(responseBody); Bundle bundle = new Bundle(); String keys[] = new String[]{"post_id", "category", "message", "team_id", "channel_id", "channel_name", "type", "sender_id", "sender_name", "version"}; diff --git a/ios/NotificationService/NotificationService.swift b/ios/NotificationService/NotificationService.swift index 19cb6e462..1f5e8b2f0 100644 --- a/ios/NotificationService/NotificationService.swift +++ b/ios/NotificationService/NotificationService.swift @@ -15,6 +15,7 @@ class NotificationService: UNNotificationServiceExtension { func fetchReceipt(notificationId: String, receivedAt: Int, type: String, postId: String, idLoaded: Bool ) -> Void { if (self.retryIndex >= fibonacciBackoffsInSeconds.count) { + contentHandler(self.bestAttemptContent!) return } @@ -23,7 +24,12 @@ class NotificationService: UNNotificationServiceExtension { receivedAt: receivedAt, type: type, postId: postId, - idLoaded: idLoaded) { data, error in + idLoaded: idLoaded) { data, response, error in + if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 { + contentHandler(self.bestAttemptContent!) + return + } + guard let data = data, error == nil else { if (idLoaded) { // Receipt retrieval failed. Kick off retries. diff --git a/ios/UploadAttachments/UploadAttachments/UploadSession.swift b/ios/UploadAttachments/UploadAttachments/UploadSession.swift index 5b59da853..6236c54a7 100644 --- a/ios/UploadAttachments/UploadAttachments/UploadSession.swift +++ b/ios/UploadAttachments/UploadAttachments/UploadSession.swift @@ -130,10 +130,10 @@ import os.log } public func notificationReceipt(notificationId: Any?, receivedAt: Int, type: Any?) { - notificationReceipt(notificationId:notificationId, receivedAt:receivedAt, type:type, postId:nil, idLoaded: false, completion:{_, _ in}) + notificationReceipt(notificationId:notificationId, receivedAt:receivedAt, type:type, postId:nil, idLoaded:false, completion:{_, _, _ in}) } - public func notificationReceipt(notificationId: Any?, receivedAt: Int, type: Any?, postId: Any? = nil, idLoaded: Bool, completion: @escaping (Data?, Error?) -> Void) { + public func notificationReceipt(notificationId: Any?, receivedAt: Int, type: Any?, postId: Any? = nil, idLoaded: Bool, completion: @escaping (Data?, URLResponse?, Error?) -> Void) { if (notificationId != nil) { let store = StoreManager.shared() as StoreManager let entities = store.getEntities(true) @@ -162,8 +162,8 @@ import os.log request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") request.httpBody = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) - let task = URLSession(configuration: .ephemeral).dataTask(with: request) { data, _, error in - completion(data, error) + let task = URLSession(configuration: .ephemeral).dataTask(with: request) { data, response, error in + completion(data, response, error) } task.resume() }