FIRMessagingClient.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/FIRMessaging.h>
  17. @class GULReachabilityChecker;
  18. @class GPBMessage;
  19. @class FIRMessagingConnection;
  20. @class FIRMessagingDataMessageManager;
  21. @class FIRMessagingRmqManager;
  22. /**
  23. * Callback to handle MCS connection requests.
  24. *
  25. * @param error The error object if any while trying to connect with MCS else nil.
  26. */
  27. typedef void (^FIRMessagingConnectCompletionHandler)(NSError *error);
  28. @protocol FIRMessagingClientDelegate <NSObject>
  29. @end
  30. /**
  31. * The client handles the subscribe/unsubscribe for an unregistered senderID
  32. * and device. It also manages the FIRMessaging data connection, the exponential backoff
  33. * algorithm in case of registration failures, sign in failures and unregister
  34. * failures. It also handles the reconnect logic if the FIRMessaging connection is
  35. * broken off by some error during an active session.
  36. */
  37. @interface FIRMessagingClient : NSObject
  38. @property(nonatomic, readonly, strong) FIRMessagingConnection *connection;
  39. @property(nonatomic, readwrite, weak) FIRMessagingDataMessageManager *dataMessageManager;
  40. // Designated initializer
  41. - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
  42. reachability:(GULReachabilityChecker *)reachability
  43. rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager;
  44. - (void)teardown;
  45. #pragma mark - MCS Connection
  46. /**
  47. * Create a MCS connection.
  48. *
  49. * @param handler The handler to be invokend once the connection is setup. If
  50. * setting up the connection fails we invoke the handler with
  51. * an appropriate error object.
  52. */
  53. - (void)connectWithHandler:(FIRMessagingConnectCompletionHandler)handler;
  54. /**
  55. * Disconnect the current MCS connection. If there is no valid connection this
  56. * should be a NO-OP.
  57. */
  58. - (void)disconnect;
  59. #pragma mark - MCS Connection State
  60. /**
  61. * If we are connected to MCS or not. This doesn't take into account the fact if
  62. * the client has been signed in(verified) by MCS.
  63. *
  64. * @return YES if we are signed in or connecting and trying to sign-in else NO.
  65. */
  66. @property(nonatomic, readonly) BOOL isConnected;
  67. /**
  68. * If we have an active MCS connection
  69. *
  70. * @return YES if we have an active MCS connection else NO.
  71. */
  72. @property(nonatomic, readonly) BOOL isConnectionActive;
  73. /**
  74. * If we should be connected to MCS
  75. *
  76. * @return YES if we have attempted a connection and not requested to disconect.
  77. */
  78. @property(nonatomic, readonly) BOOL shouldStayConnected;
  79. /**
  80. * Schedule a retry to connect to MCS. If `immediately` is `YES` try to
  81. * schedule a retry now else retry with some delay.
  82. *
  83. * @param immediately Should retry right now.
  84. */
  85. - (void)retryConnectionImmediately:(BOOL)immediately;
  86. #pragma mark - Messages
  87. /**
  88. * Send a message over the MCS connection.
  89. *
  90. * @param message Message to be sent.
  91. */
  92. - (void)sendMessage:(GPBMessage *)message;
  93. /**
  94. * Send message if we have an active MCS connection. If not cache the message
  95. * for this session and in case we are able to re-establish the connection try
  96. * again else drop it. This should only be used for TTL=0 messages for now.
  97. *
  98. * @param message Message to be sent.
  99. */
  100. - (void)sendOnConnectOrDrop:(GPBMessage *)message;
  101. @end