Без опису

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "FIRInstanceIDAPNSInfo.h"
  17. #import "FIRInstanceIDConstants.h"
  18. /// The key used to find the APNs device token in an archive.
  19. NSString *const kFIRInstanceIDAPNSInfoTokenKey = @"device_token";
  20. /// The key used to find the sandbox value in an archive.
  21. NSString *const kFIRInstanceIDAPNSInfoSandboxKey = @"sandbox";
  22. @implementation FIRInstanceIDAPNSInfo
  23. - (instancetype)initWithDeviceToken:(NSData *)deviceToken isSandbox:(BOOL)isSandbox {
  24. self = [super init];
  25. if (self) {
  26. _deviceToken = [deviceToken copy];
  27. _sandbox = isSandbox;
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithTokenOptionsDictionary:(NSDictionary *)dictionary {
  32. id deviceToken = dictionary[kFIRInstanceIDTokenOptionsAPNSKey];
  33. if (![deviceToken isKindOfClass:[NSData class]]) {
  34. return nil;
  35. }
  36. id isSandbox = dictionary[kFIRInstanceIDTokenOptionsAPNSIsSandboxKey];
  37. if (![isSandbox isKindOfClass:[NSNumber class]]) {
  38. return nil;
  39. }
  40. self = [super init];
  41. if (self) {
  42. _deviceToken = (NSData *)deviceToken;
  43. _sandbox = ((NSNumber *)isSandbox).boolValue;
  44. }
  45. return self;
  46. }
  47. #pragma mark - NSCoding
  48. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  49. id deviceToken = [aDecoder decodeObjectForKey:kFIRInstanceIDAPNSInfoTokenKey];
  50. if (![deviceToken isKindOfClass:[NSData class]]) {
  51. return nil;
  52. }
  53. BOOL isSandbox = [aDecoder decodeBoolForKey:kFIRInstanceIDAPNSInfoSandboxKey];
  54. return [self initWithDeviceToken:(NSData *)deviceToken isSandbox:isSandbox];
  55. }
  56. - (void)encodeWithCoder:(NSCoder *)aCoder {
  57. [aCoder encodeObject:self.deviceToken forKey:kFIRInstanceIDAPNSInfoTokenKey];
  58. [aCoder encodeBool:self.sandbox forKey:kFIRInstanceIDAPNSInfoSandboxKey];
  59. }
  60. - (BOOL)isEqualToAPNSInfo:(FIRInstanceIDAPNSInfo *)otherInfo {
  61. if ([super isEqual:otherInfo]) {
  62. return YES;
  63. }
  64. return ([self.deviceToken isEqualToData:otherInfo.deviceToken] &&
  65. self.isSandbox == otherInfo.isSandbox);
  66. }
  67. @end