FIRMessagingReceiver.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017 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 "FIRMessagingReceiver.h"
  17. #import <UIKit/UIKit.h>
  18. #import "FIRMessaging.h"
  19. #import "FIRMessagingLogger.h"
  20. #import "FIRMessagingUtilities.h"
  21. #import "FIRMessaging_Private.h"
  22. static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
  23. static NSString *const kUpstreamErrorUserInfoKey = @"error";
  24. static int downstreamMessageID = 0;
  25. @interface FIRMessagingReceiver ()
  26. @property(nonatomic, strong) GULUserDefaults *defaults;
  27. @end
  28. @implementation FIRMessagingReceiver
  29. #pragma mark - FIRMessagingDataMessageManager protocol
  30. - (void)didReceiveMessage:(NSDictionary *)message withIdentifier:(nullable NSString *)messageID {
  31. if (![messageID length]) {
  32. messageID = [[self class] nextMessageID];
  33. }
  34. [self handleDirectChannelMessage:message withIdentifier:messageID];
  35. }
  36. - (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
  37. NSNotification *notification;
  38. if (error) {
  39. NSDictionary *userInfo = @{
  40. kUpstreamMessageIDUserInfoKey : [messageID copy],
  41. kUpstreamErrorUserInfoKey : error
  42. };
  43. notification = [NSNotification notificationWithName:FIRMessagingSendErrorNotification
  44. object:nil
  45. userInfo:userInfo];
  46. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  47. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver000,
  48. @"Fail to send upstream message: %@ error: %@", messageID, error);
  49. } else {
  50. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver001, @"Will send upstream message: %@",
  51. messageID);
  52. }
  53. }
  54. - (void)didSendDataMessageWithID:(NSString *)messageID {
  55. // invoke the callbacks asynchronously
  56. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver002, @"Did send upstream message: %@",
  57. messageID);
  58. NSNotification * notification =
  59. [NSNotification notificationWithName:FIRMessagingSendSuccessNotification
  60. object:nil
  61. userInfo:@{ kUpstreamMessageIDUserInfoKey : [messageID copy] }];
  62. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  63. }
  64. - (void)didDeleteMessagesOnServer {
  65. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver003,
  66. @"Will send deleted messages notification");
  67. NSNotification * notification =
  68. [NSNotification notificationWithName:FIRMessagingMessagesDeletedNotification
  69. object:nil];
  70. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  71. }
  72. #pragma mark - Private Helpers
  73. - (void)handleDirectChannelMessage:(NSDictionary *)message withIdentifier:(NSString *)messageID {
  74. FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
  75. wrappedMessage.appData = [message copy];
  76. wrappedMessage.messageID = messageID;
  77. [self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
  78. }
  79. + (NSString *)nextMessageID {
  80. @synchronized (self) {
  81. ++downstreamMessageID;
  82. return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
  83. }
  84. }
  85. @end