You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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 "Firebase/Messaging/FIRMessagingReceiver.h"
  17. #import <FirebaseMessaging/FIRMessaging.h>
  18. #import "Firebase/Messaging/FIRMessagingLogger.h"
  19. #import "Firebase/Messaging/FIRMessagingUtilities.h"
  20. #import "Firebase/Messaging/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],
  37. kUpstreamErrorUserInfoKey : error
  38. };
  39. notification = [NSNotification notificationWithName:FIRMessagingSendErrorNotification
  40. object:nil
  41. userInfo:userInfo];
  42. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  43. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver000,
  44. @"Fail to send upstream message: %@ error: %@", messageID, error);
  45. } else {
  46. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver001, @"Will send upstream message: %@",
  47. messageID);
  48. }
  49. }
  50. - (void)didSendDataMessageWithID:(NSString *)messageID {
  51. // invoke the callbacks asynchronously
  52. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver002, @"Did send upstream message: %@",
  53. messageID);
  54. NSNotification * notification =
  55. [NSNotification notificationWithName:FIRMessagingSendSuccessNotification
  56. object:nil
  57. userInfo:@{ kUpstreamMessageIDUserInfoKey : [messageID copy] }];
  58. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  59. }
  60. - (void)didDeleteMessagesOnServer {
  61. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver003,
  62. @"Will send deleted messages notification");
  63. NSNotification * notification =
  64. [NSNotification notificationWithName:FIRMessagingMessagesDeletedNotification
  65. object:nil];
  66. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  67. }
  68. #pragma mark - Private Helpers
  69. - (void)handleDirectChannelMessage:(NSDictionary *)message withIdentifier:(NSString *)messageID {
  70. FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
  71. wrappedMessage.appData = [message copy];
  72. wrappedMessage.messageID = messageID;
  73. [self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
  74. }
  75. + (NSString *)nextMessageID {
  76. @synchronized (self) {
  77. ++downstreamMessageID;
  78. return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
  79. }
  80. }
  81. @end