1 |
efrain |
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 |
|
|
|
17 |
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
|
|
|
18 |
|
|
|
19 |
NS_ASSUME_NONNULL_BEGIN
|
|
|
20 |
|
|
|
21 |
@class FIRMessagingTokenManager;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* FIRMessagingPubSub provides a publish-subscribe model for sending FIRMessaging topic messages.
|
|
|
25 |
*
|
|
|
26 |
* An app can subscribe to different topics defined by the
|
|
|
27 |
* developer. The app server can then send messages to the subscribed devices
|
|
|
28 |
* without having to maintain topic-subscribers mapping. Topics do not
|
|
|
29 |
* need to be explicitly created before subscribing or publishing—they
|
|
|
30 |
* are automatically created when publishing or subscribing.
|
|
|
31 |
*
|
|
|
32 |
* Messages published to the topic will be received as regular FIRMessaging messages
|
|
|
33 |
* with `"from"` set to `"/topics/myTopic"`.
|
|
|
34 |
*
|
|
|
35 |
* Only topic names that match the pattern `"/topics/[a-zA-Z0-9-_.~%]{1,900}"`
|
|
|
36 |
* are allowed for subscribing and publishing.
|
|
|
37 |
*/
|
|
|
38 |
@interface FIRMessagingPubSub : NSObject
|
|
|
39 |
|
|
|
40 |
- (instancetype)initWithTokenManager:(FIRMessagingTokenManager *)tokenManager;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Subscribes an app instance to a topic, enabling it to receive messages
|
|
|
44 |
* sent to that topic.
|
|
|
45 |
*
|
|
|
46 |
* This is an asynchronous call. If subscription fails, FIRMessaging
|
|
|
47 |
* invokes the completion callback with the appropriate error.
|
|
|
48 |
*
|
|
|
49 |
* @see FIRMessagingPubSub unsubscribeWithToken:topic:handler:
|
|
|
50 |
*
|
|
|
51 |
* @param token The registration token as received from the InstanceID
|
|
|
52 |
* library for a given `authorizedEntity` and "gcm" scope.
|
|
|
53 |
* @param topic The topic to subscribe to. Should be of the form
|
|
|
54 |
* `"/topics/<topic-name>"`.
|
|
|
55 |
* @param options Unused parameter, please pass nil or empty dictionary.
|
|
|
56 |
* @param handler The callback handler invoked when the subscribe call
|
|
|
57 |
* ends. In case of success, a nil error is returned. Otherwise,
|
|
|
58 |
* an appropriate error object is returned.
|
|
|
59 |
* @discussion This method is thread-safe. However, it is not guaranteed to
|
|
|
60 |
* return on the main thread.
|
|
|
61 |
*/
|
|
|
62 |
- (void)subscribeWithToken:(NSString *)token
|
|
|
63 |
topic:(NSString *)topic
|
|
|
64 |
options:(nullable NSDictionary *)options
|
|
|
65 |
handler:(FIRMessagingTopicOperationCompletion)handler;
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Unsubscribes an app instance from a topic, stopping it from receiving
|
|
|
69 |
* any further messages sent to that topic.
|
|
|
70 |
*
|
|
|
71 |
* This is an asynchronous call. If the attempt to unsubscribe fails,
|
|
|
72 |
* we invoke the `completion` callback passed in with an appropriate error.
|
|
|
73 |
*
|
|
|
74 |
* @param token The token used to subscribe to this topic.
|
|
|
75 |
* @param topic The topic to unsubscribe from. Should be of the form
|
|
|
76 |
* `"/topics/<topic-name>"`.
|
|
|
77 |
* @param options Unused parameter, please pass nil or empty dictionary.
|
|
|
78 |
* @param handler The handler that is invoked once the unsubscribe call ends.
|
|
|
79 |
* In case of success, nil error is returned. Otherwise, an
|
|
|
80 |
* appropriate error object is returned.
|
|
|
81 |
* @discussion This method is thread-safe. However, it is not guaranteed to
|
|
|
82 |
* return on the main thread.
|
|
|
83 |
*/
|
|
|
84 |
- (void)unsubscribeWithToken:(NSString *)token
|
|
|
85 |
topic:(NSString *)topic
|
|
|
86 |
options:(nullable NSDictionary *)options
|
|
|
87 |
handler:(FIRMessagingTopicOperationCompletion)handler;
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Asynchronously subscribe to the topic. Adds to the pending list of topic operations.
|
|
|
91 |
* Retry in case of failures. This makes a repeated attempt to subscribe to the topic
|
|
|
92 |
* as compared to the `subscribe` method above which tries once.
|
|
|
93 |
*
|
|
|
94 |
* @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
|
|
|
95 |
* @param handler The handler that is invoked once the unsubscribe call ends.
|
|
|
96 |
* In case of success, nil error is returned. Otherwise, an
|
|
|
97 |
* appropriate error object is returned.
|
|
|
98 |
*/
|
|
|
99 |
- (void)subscribeToTopic:(NSString *)topic
|
|
|
100 |
handler:(nullable FIRMessagingTopicOperationCompletion)handler;
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
|
|
|
104 |
* Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
|
|
|
105 |
* as compared to the `unsubscribe` method above which tries once.
|
|
|
106 |
*
|
|
|
107 |
* @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
|
|
|
108 |
* @param handler The handler that is invoked once the unsubscribe call ends.
|
|
|
109 |
* In case of success, nil error is returned. Otherwise, an
|
|
|
110 |
* appropriate error object is returned.
|
|
|
111 |
*/
|
|
|
112 |
- (void)unsubscribeFromTopic:(NSString *)topic
|
|
|
113 |
handler:(nullable FIRMessagingTopicOperationCompletion)handler;
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Schedule subscriptions sync.
|
|
|
117 |
*
|
|
|
118 |
* @param immediately YES if the sync should be scheduled immediately else NO if we can delay
|
|
|
119 |
* the sync.
|
|
|
120 |
*/
|
|
|
121 |
- (void)scheduleSync:(BOOL)immediately;
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Adds the "/topics/" prefix to the topic.
|
|
|
125 |
*
|
|
|
126 |
* @param topic The topic to add the prefix to.
|
|
|
127 |
*
|
|
|
128 |
* @return The new topic name with the "/topics/" prefix added.
|
|
|
129 |
*/
|
|
|
130 |
+ (NSString *)addPrefixToTopic:(NSString *)topic;
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Removes the "/topics/" prefix from the topic.
|
|
|
134 |
*
|
|
|
135 |
* @param topic The topic to remove the prefix from.
|
|
|
136 |
*
|
|
|
137 |
* @return The new topic name with the "/topics/" prefix removed.
|
|
|
138 |
*/
|
|
|
139 |
|
|
|
140 |
+ (NSString *)removePrefixFromTopic:(NSString *)topic;
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Check if the topic name has "/topics/" prefix.
|
|
|
144 |
*
|
|
|
145 |
* @param topic The topic name to verify.
|
|
|
146 |
*
|
|
|
147 |
* @return YES if the topic name has "/topics/" prefix else NO.
|
|
|
148 |
*/
|
|
|
149 |
+ (BOOL)hasTopicsPrefix:(NSString *)topic;
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Check if it's a valid topic name. This includes "/topics/" prefix in the topic name.
|
|
|
153 |
*
|
|
|
154 |
* @param topic The topic name to verify.
|
|
|
155 |
*
|
|
|
156 |
* @return YES if the topic name satisfies the regex "/topics/[a-zA-Z0-9-_.~%]{1,900}".
|
|
|
157 |
*/
|
|
|
158 |
+ (BOOL)isValidTopicWithPrefix:(NSString *)topic;
|
|
|
159 |
|
|
|
160 |
@end
|
|
|
161 |
|
|
|
162 |
NS_ASSUME_NONNULL_END
|