FIRMessagingAPNSInfo.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 - NSSecureCoding
  62. + (BOOL)supportsSecureCoding {
  63. return YES;
  64. }
  65. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  66. NSData *deviceToken = [aDecoder decodeObjectOfClass:[NSData class]
  67. forKey:kFIRInstanceIDAPNSInfoTokenKey];
  68. if (!deviceToken) {
  69. return nil;
  70. }
  71. BOOL isSandbox = [aDecoder decodeBoolForKey:kFIRInstanceIDAPNSInfoSandboxKey];
  72. return [self initWithDeviceToken:(NSData *)deviceToken isSandbox:isSandbox];
  73. }
  74. - (void)encodeWithCoder:(NSCoder *)aCoder {
  75. [aCoder encodeObject:self.deviceToken forKey:kFIRInstanceIDAPNSInfoTokenKey];
  76. [aCoder encodeBool:self.sandbox forKey:kFIRInstanceIDAPNSInfoSandboxKey];
  77. }
  78. - (BOOL)isEqualToAPNSInfo:(FIRMessagingAPNSInfo *)otherInfo {
  79. return ([self.deviceToken isEqualToData:otherInfo.deviceToken] &&
  80. self.isSandbox == otherInfo.isSandbox);
  81. }
  82. @end