FIRMessagingAPNSInfo.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/FIRMessagingAPNSInfo.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  18. /// The key used to find the APNs device token in an archive.
  19. static NSString *const kFIRInstanceIDAPNSInfoTokenKey = @"device_token";
  20. /// The key used to find the sandbox value in an archive.
  21. static NSString *const kFIRInstanceIDAPNSInfoSandboxKey = @"sandbox";
  22. @interface FIRMessagingAPNSInfo ()
  23. /// The APNs device token, provided by the OS to the application delegate
  24. @property(nonatomic, copy) NSData *deviceToken;
  25. /// Represents whether or not this is deviceToken is for the sandbox
  26. /// environment, or production.
  27. @property(nonatomic, getter=isSandbox) BOOL sandbox;
  28. @end
  29. @implementation FIRMessagingAPNSInfo
  30. - (instancetype)initWithDeviceToken:(NSData *)deviceToken isSandbox:(BOOL)isSandbox {
  31. self = [super init];
  32. if (self) {
  33. _deviceToken = [deviceToken copy];
  34. _sandbox = isSandbox;
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithTokenOptionsDictionary:(NSDictionary *)dictionary {
  39. id deviceToken = dictionary[kFIRMessagingTokenOptionsAPNSKey];
  40. if (![deviceToken isKindOfClass:[NSData class]]) {
  41. return nil;
  42. }
  43. id isSandbox = dictionary[kFIRMessagingTokenOptionsAPNSIsSandboxKey];
  44. if (![isSandbox isKindOfClass:[NSNumber class]]) {
  45. return nil;
  46. }
  47. self = [super init];
  48. if (self) {
  49. _deviceToken = (NSData *)deviceToken;
  50. _sandbox = ((NSNumber *)isSandbox).boolValue;
  51. }
  52. return self;
  53. }
  54. #pragma mark - NSCopying
  55. - (id)copyWithZone:(NSZone *)zone {
  56. FIRMessagingAPNSInfo *clone = [[FIRMessagingAPNSInfo alloc] init];
  57. clone.deviceToken = [_deviceToken copy];
  58. clone.sandbox = _sandbox;
  59. return clone;
  60. }
  61. #pragma mark - NSCoding
  62. + (BOOL)supportsSecureCoding {
  63. return YES;
  64. }
  65. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  66. id deviceToken = [aDecoder decodeObjectForKey:kFIRInstanceIDAPNSInfoTokenKey];
  67. if (![deviceToken isKindOfClass:[NSData class]]) {
  68. return nil;
  69. }
  70. BOOL isSandbox = [aDecoder decodeBoolForKey:kFIRInstanceIDAPNSInfoSandboxKey];
  71. return [self initWithDeviceToken:(NSData *)deviceToken isSandbox:isSandbox];
  72. }
  73. - (void)encodeWithCoder:(NSCoder *)aCoder {
  74. [aCoder encodeObject:self.deviceToken forKey:kFIRInstanceIDAPNSInfoTokenKey];
  75. [aCoder encodeBool:self.sandbox forKey:kFIRInstanceIDAPNSInfoSandboxKey];
  76. }
  77. - (BOOL)isEqualToAPNSInfo:(FIRMessagingAPNSInfo *)otherInfo {
  78. return ([self.deviceToken isEqualToData:otherInfo.deviceToken] &&
  79. self.isSandbox == otherInfo.isSandbox);
  80. }
  81. @end