2 * Copyright 2017 Google
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import "FIRMessaging.h"
19 @class GULReachabilityChecker;
22 @class FIRMessagingConnection;
23 @class FIRMessagingDataMessageManager;
24 @class FIRMessagingRmqManager;
27 * Callback to handle MCS connection requests.
29 * @param error The error object if any while trying to connect with MCS else nil.
31 typedef void(^FIRMessagingConnectCompletionHandler)(NSError *error);
33 @protocol FIRMessagingClientDelegate <NSObject>
38 * The client handles the subscribe/unsubscribe for an unregistered senderID
39 * and device. It also manages the FIRMessaging data connection, the exponential backoff
40 * algorithm in case of registration failures, sign in failures and unregister
41 * failures. It also handles the reconnect logic if the FIRMessaging connection is
42 * broken off by some error during an active session.
44 @interface FIRMessagingClient : NSObject
46 @property(nonatomic, readonly, strong) FIRMessagingConnection *connection;
47 @property(nonatomic, readwrite, weak) FIRMessagingDataMessageManager *dataMessageManager;
49 // Designated initializer
50 - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
51 reachability:(GULReachabilityChecker *)reachability
52 rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager;
56 - (void)cancelAllRequests;
58 #pragma mark - FIRMessaging subscribe
61 * Update the subscription associated with the given token and topic.
63 * For a to-be-created subscription we check if the client is already
64 * subscribed to the topic or not. If subscribed we should have the
65 * subscriptionID in the cache and we return from there itself, else we call
66 * the FIRMessaging backend to create a new subscription for the topic for this client.
68 * For delete subscription requests we delete the stored subscription in the
69 * client and then invoke the FIRMessaging backend to delete the existing subscription
72 * @param token The token associated with the device.
73 * @param topic The topic for which the subscription should be updated.
74 * @param options The options to be passed in to the subscription request.
75 * @param shouldDelete If YES this would delete the subscription from the cache
76 * and also let the FIRMessaging backend know that we need to delete
77 * the subscriptionID associated with this topic.
78 * If NO we try to create a new subscription for the given
80 * @param handler The handler to invoke once the subscription request
83 - (void)updateSubscriptionWithToken:(NSString *)token
84 topic:(NSString *)topic
85 options:(NSDictionary *)options
86 shouldDelete:(BOOL)shouldDelete
87 handler:(FIRMessagingTopicOperationCompletion)handler;
89 #pragma mark - MCS Connection
92 * Create a MCS connection.
94 * @param handler The handler to be invokend once the connection is setup. If
95 * setting up the connection fails we invoke the handler with
96 * an appropriate error object.
98 - (void)connectWithHandler:(FIRMessagingConnectCompletionHandler)handler;
101 * Disconnect the current MCS connection. If there is no valid connection this
106 #pragma mark - MCS Connection State
109 * If we are connected to MCS or not. This doesn't take into account the fact if
110 * the client has been signed in(verified) by MCS.
112 * @return YES if we are signed in or connecting and trying to sign-in else NO.
114 @property(nonatomic, readonly) BOOL isConnected;
117 * If we have an active MCS connection
119 * @return YES if we have an active MCS connection else NO.
121 @property(nonatomic, readonly) BOOL isConnectionActive;
124 * If we should be connected to MCS
126 * @return YES if we have attempted a connection and not requested to disconect.
128 @property(nonatomic, readonly) BOOL shouldStayConnected;
131 * Schedule a retry to connect to MCS. If `immediately` is `YES` try to
132 * schedule a retry now else retry with some delay.
134 * @param immediately Should retry right now.
136 - (void)retryConnectionImmediately:(BOOL)immediately;
138 #pragma mark - Messages
141 * Send a message over the MCS connection.
143 * @param message Message to be sent.
145 - (void)sendMessage:(GPBMessage *)message;
148 * Send message if we have an active MCS connection. If not cache the message
149 * for this session and in case we are able to re-establish the connection try
150 * again else drop it. This should only be used for TTL=0 messages for now.
152 * @param message Message to be sent.
154 - (void)sendOnConnectOrDrop:(GPBMessage *)message;