FIRMessagingTokenDeleteOperation.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenDeleteOperation.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  21. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  22. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  23. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenOperation.h"
  24. @implementation FIRMessagingTokenDeleteOperation
  25. - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity
  26. scope:(NSString *)scope
  27. checkinPreferences:(FIRMessagingCheckinPreferences *)checkinPreferences
  28. instanceID:(NSString *)instanceID
  29. action:(FIRMessagingTokenAction)action
  30. heartbeatLogger:(id<FIRHeartbeatLoggerProtocol>)heartbeatLogger {
  31. return [super initWithAction:action
  32. forAuthorizedEntity:authorizedEntity
  33. scope:scope
  34. options:nil
  35. checkinPreferences:checkinPreferences
  36. instanceID:instanceID
  37. heartbeatLogger:heartbeatLogger];
  38. }
  39. - (void)performTokenOperation {
  40. NSMutableURLRequest *request = [self tokenRequest];
  41. // Build form-encoded body
  42. NSString *deviceAuthID = self.checkinPreferences.deviceID;
  43. NSMutableArray<NSURLQueryItem *> *queryItems =
  44. [FIRMessagingTokenOperation standardQueryItemsWithDeviceID:deviceAuthID scope:self.scope];
  45. [queryItems addObject:[NSURLQueryItem queryItemWithName:@"delete" value:@"true"]];
  46. if (self.action == FIRMessagingTokenActionDeleteTokenAndIID) {
  47. [queryItems addObject:[NSURLQueryItem queryItemWithName:@"iid-operation" value:@"delete"]];
  48. }
  49. if (self.authorizedEntity) {
  50. [queryItems addObject:[NSURLQueryItem queryItemWithName:@"sender" value:self.authorizedEntity]];
  51. }
  52. // Typically we include our public key-signed url items, but in some cases (like deleting all FCM
  53. // tokens), we don't.
  54. if (self.instanceID.length > 0) {
  55. [queryItems addObject:[NSURLQueryItem queryItemWithName:kFIRMessagingParamInstanceID
  56. value:self.instanceID]];
  57. }
  58. NSURLComponents *components = [[NSURLComponents alloc] init];
  59. components.queryItems = queryItems;
  60. NSString *content = components.query;
  61. request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
  62. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationFetchRequest,
  63. @"Unregister request to %@ content: %@",
  64. FIRMessagingTokenRegisterServer(), content);
  65. FIRMessaging_WEAKIFY(self);
  66. void (^requestHandler)(NSData *, NSURLResponse *, NSError *) =
  67. ^(NSData *data, NSURLResponse *response, NSError *error) {
  68. FIRMessaging_STRONGIFY(self);
  69. [self handleResponseWithData:data response:response error:error];
  70. };
  71. NSURLSessionConfiguration *config = NSURLSessionConfiguration.defaultSessionConfiguration;
  72. config.timeoutIntervalForResource = 60.0f; // 1 minute
  73. NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
  74. self.dataTask = [session dataTaskWithRequest:request completionHandler:requestHandler];
  75. [self.dataTask resume];
  76. }
  77. - (void)handleResponseWithData:(NSData *)data
  78. response:(NSURLResponse *)response
  79. error:(NSError *)error {
  80. if (error) {
  81. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationRequestError,
  82. @"Device unregister HTTP fetch error. Error code: %ld",
  83. (long)error.code);
  84. [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  85. return;
  86. }
  87. NSString *dataResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  88. if (dataResponse.length == 0) {
  89. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  90. failureReason:@"Empty response."];
  91. [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  92. return;
  93. }
  94. if (![dataResponse hasPrefix:@"deleted="] && ![dataResponse hasPrefix:@"token="]) {
  95. NSString *failureReason =
  96. [NSString stringWithFormat:@"Invalid unregister response %@", response];
  97. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenDeleteOperationBadResponse, @"%@",
  98. failureReason);
  99. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  100. failureReason:failureReason];
  101. [self finishWithResult:FIRMessagingTokenOperationError token:nil error:error];
  102. return;
  103. }
  104. [self finishWithResult:FIRMessagingTokenOperationSucceeded token:nil error:nil];
  105. }
  106. @end