FIRMessagingReceiver.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "FirebaseMessaging/Sources/FIRMessagingReceiver.h"
  17. #import <FirebaseMessaging/FIRMessaging.h>
  18. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  20. #import "FirebaseMessaging/Sources/FIRMessaging_Private.h"
  21. static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
  22. static NSString *const kUpstreamErrorUserInfoKey = @"error";
  23. static int downstreamMessageID = 0;
  24. @implementation FIRMessagingReceiver
  25. #pragma mark - FIRMessagingDataMessageManager protocol
  26. - (void)didReceiveMessage:(NSDictionary *)message withIdentifier:(nullable NSString *)messageID {
  27. if (![messageID length]) {
  28. messageID = [[self class] nextMessageID];
  29. }
  30. [self handleDirectChannelMessage:message withIdentifier:messageID];
  31. }
  32. - (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
  33. NSNotification *notification;
  34. if (error) {
  35. NSDictionary *userInfo =
  36. @{kUpstreamMessageIDUserInfoKey : [messageID copy], kUpstreamErrorUserInfoKey : error};
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  39. notification = [NSNotification notificationWithName:FIRMessagingSendErrorNotification
  40. object:nil
  41. userInfo:userInfo];
  42. #pragma clang diagnostic pop
  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. #pragma clang diagnostic push
  56. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  57. NSNotification *notification =
  58. [NSNotification notificationWithName:FIRMessagingSendSuccessNotification
  59. object:nil
  60. userInfo:@{kUpstreamMessageIDUserInfoKey : [messageID copy]}];
  61. #pragma clang diagnostic pop
  62. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  63. }
  64. - (void)didDeleteMessagesOnServer {
  65. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver003,
  66. @"Will send deleted messages notification");
  67. #pragma clang diagnostic push
  68. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  69. NSNotification *notification =
  70. [NSNotification notificationWithName:FIRMessagingMessagesDeletedNotification object:nil];
  71. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  72. }
  73. #pragma mark - Private Helpers
  74. - (void)handleDirectChannelMessage:(NSDictionary *)message withIdentifier:(NSString *)messageID {
  75. #pragma clang diagnostic push
  76. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  77. FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
  78. wrappedMessage.appData = [message copy];
  79. wrappedMessage.messageID = messageID;
  80. [self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
  81. #pragma clang diagnostic pop
  82. }
  83. + (NSString *)nextMessageID {
  84. @synchronized(self) {
  85. ++downstreamMessageID;
  86. return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
  87. }
  88. }
  89. @end