FIRMessagingReceiver.m 3.7 KB

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