29 |
30 | ///---------------------------------------------------------------------------------------
31 | /// @name Instance Methods
32 | ///--------------------------------------------------------------------------------------
33 | /**
34 | * Occurs when the client connects.
35 | *
36 | * @param ortc The ORTC object.
37 | */
38 | - (void)onConnected:(OrtcClient*) ortc;
39 | /**
40 | * Occurs when the client disconnects.
41 | *
42 | * @param ortc The ORTC object.
43 | */
44 | - (void)onDisconnected:(OrtcClient*) ortc;
45 | /**
46 | * Occurs when the client subscribes to a channel.
47 | *
48 | * @param ortc The ORTC object.
49 | * @param channel The channel name.
50 | */
51 | - (void)onSubscribed:(OrtcClient*) ortc channel:(NSString*) channel;
52 | /**
53 | * Occurs when the client unsubscribes from a channel.
54 | *
55 | * @param ortc The ORTC object.
56 | * @param channel The channel name.
57 | */
58 | - (void)onUnsubscribed:(OrtcClient*) ortc channel:(NSString*) channel;
59 | /**
60 | * Occurs when there is an exception.
61 | *
62 | * @param ortc The ORTC object.
63 | * @param error The occurred exception.
64 | */
65 | - (void)onException:(OrtcClient*) ortc error:(NSError*) error;
66 | /**
67 | * Occurs when the client attempts to reconnect.
68 | *
69 | * @param ortc The ORTC object.
70 | */
71 | - (void)onReconnecting:(OrtcClient*) ortc;
72 | /**
73 | * Occurs when the client reconnects.
74 | *
75 | * @param ortc The ORTC object.
76 | */
77 | - (void)onReconnected:(OrtcClient*) ortc;
78 | /**
79 | * Occurs when the client enables presence.
80 | *
81 | * @param error Description of error if occurs.
82 | * @param result Result of enablePresence
83 | */
84 | //- (void)onEnablePresence:(NSError*) error result:(NSString*) result;
85 |
86 |
87 | @end
88 |
89 | /**
90 |
91 | OrtcClient Usage - Code Example
92 |
93 | - ViewController.h
94 |
95 | #import "OrtcClient.h"
96 | @interface ViewController : UIViewController
97 | {
98 | @private
99 |
100 | OrtcClient* ortcClient;
101 | void (^onMessage)(OrtcClient* ortc, NSString* channel, NSString* message);
102 | // ...
103 | }
104 | // ...
105 | @end
106 |
107 |
108 |
109 |
110 |
111 | - ViewController.m
112 |
113 |
114 | #import "ViewController.h"
115 | @implementation ViewController
116 |
117 | - (void)viewDidLoad
118 | {
119 | [super viewDidLoad];
120 |
121 | // Instantiate OrtcClient
122 | ortcClient = [OrtcClient ortcClientWithConfig:self];
123 |
124 | // Post permissions
125 | @try {
126 | NSMutableDictionary* myPermissions = [[NSMutableDictionary alloc] init];
127 |
128 | [myPermissions setObject:@"w" forKey:@"channel1"];
129 | [myPermissions setObject:@"w" forKey:@"channel2"];
130 | [myPermissions setObject:@"r" forKey:@"channelread"];
131 |
132 | BOOL postResult = [ortcClient saveAuthentication:@"http://ortc_server"
133 | isCLuster:YES authenticationToken:@"myAuthenticationToken"
134 | authenticationTokenIsPrivate:NO applicationKey:@"myApplicationKey"
135 | timeToLive:1800 privateKey:@"myPrivateKey" permissions:myPermissions];
136 |
137 | if (postResult) {
138 | // Permissions correctly posted
139 | }
140 | else {
141 | // Unable to post permissions
142 | }
143 | }
144 | @catch (NSException* exception) {
145 | // Exception posting permissions
146 | }
147 |
148 | // Set connection properties
149 | [ortcClient setConnectionMetadata:@"clientConnMeta"];
150 | [ortcClient setClusterUrl:@"http://ortc_server"];
151 |
152 | // Connect
153 | [ortcClient connect:@"myApplicationKey" authenticationToken:@"myAuthenticationToken"];
154 | }
155 |
156 | - (void) onConnected:(OrtcClient*) ortc
157 | {
158 | // Connected
159 | onMessage = ^(OrtcClient* ortc, NSString* channel, NSString* message) {
160 | // Received message 'message' at channel 'channel'
161 | [ortcClient unsubscribe:channel];
162 | };
163 |
164 | [ortcClient subscribe:@"channel1" subscribeOnReconnected:YES onMessage:onMessage];
165 | [ortcClient subscribe:@"channel2" subscribeOnReconnected:NO onMessage:onMessage];
166 | [ortcClient subscribeWithNotifications:@"channel3" subscribeOnReconnected:YES onMessage:onMessage];
167 | }
168 |
169 | - (void) onDisconnected:(OrtcClient*) ortc
170 | {
171 | // Disconnected
172 | }
173 |
174 | - (void) onReconnecting:(OrtcClient*) ortc
175 | {
176 | // Trying to reconnect
177 | }
178 |
179 | - (void) onReconnected:(OrtcClient*) ortc
180 | {
181 | // Reconnected
182 | }
183 |
184 | - (void) onSubscribed:(OrtcClient*) ortc channel:(NSString*) channel
185 | {
186 | // Subscribed to the channel 'channel'
187 | [ortcClient send:channel message:@"Message to the channel"];
188 | }
189 |
190 | - (void) onUnsubscribed:(OrtcClient*) ortc channel:(NSString*) channel
191 | {
192 | // Unsubscribed from the channel 'channel'
193 | [ortcClient disconnect];
194 | }
195 |
196 | - (void) onException:(OrtcClient*) ortc error:(NSError*) error
197 | {
198 | // Exception occurred
199 | }
200 |
201 | @end
202 |
203 | */
204 |
205 |
206 |
207 | @interface OrtcClient : NSObject
208 |
209 | ///---------------------------------------------------------------------------------------
210 | /// @name Properties
211 | ///---------------------------------------------------------------------------------------
212 | @property (nonatomic, retain) NSString* id;
213 | @property (nonatomic, retain) NSString* url;
214 | @property (nonatomic, retain) NSString* clusterUrl;
215 | @property (nonatomic, retain) NSString* connectionMetadata;
216 | @property (nonatomic, retain) NSString* announcementSubChannel;
217 | @property (nonatomic, retain) NSString* sessionId;
218 | @property (assign) int connectionTimeout;
219 | @property (assign) BOOL isConnected;
220 |
221 |
222 | ///---------------------------------------------------------------------------------------
223 | /// @name Class Methods
224 | ///---------------------------------------------------------------------------------------
225 | /**
226 | * Initializes a new instance of the ORTC class.
227 | *
228 | * @param delegate The object holding the ORTC callbacks, usually 'self'.
229 | *
230 | * @return New instance of the ORTC class.
231 | */
232 |
233 | + (id)ortcClientWithConfig:(id) delegate;
234 |
235 |
236 | ///---------------------------------------------------------------------------------------
237 | /// @name Instance Methods
238 | ///---------------------------------------------------------------------------------------
239 | /**
240 | * Connects with the application key and authentication token.
241 | *
242 | * @param applicationKey The application key.
243 | * @param authenticationToken The authentication token.
244 | */
245 | - (void)connect:(NSString*) applicationKey authenticationToken:(NSString*) authenticationToken;
246 | /**
247 | * Sends a message to a channel.
248 | *
249 | * @param channel The channel name.
250 | * @param message The message to send.
251 | */
252 | - (void)send:(NSString*) channel message:(NSString*) message;
253 | /**
254 | * Subscribes to a channel to receive messages sent to it.
255 | *
256 | * @param channel The channel name.
257 | * @param subscribeOnReconnected Indicates whether the client should subscribe to the channel when reconnected (if it was previously subscribed when connected).
258 | * @param onMessage The callback called when a message arrives at the channel.
259 | */
260 | - (void)subscribe:(NSString*) channel subscribeOnReconnected:(BOOL) aSubscribeOnReconnected onMessage:(void (^)(OrtcClient* ortc, NSString* channel, NSString* message)) onMessage;
261 |
262 | /**
263 | * Subscribes to a channel, with Push Notifications Service, to receive messages sent to it.
264 | *
265 | * @param channel The channel name. Only channels with alphanumeric name and the following characters: "_" "-" ":" are allowed.
266 | * @param subscribeOnReconnected Indicates whether the client should subscribe to the channel when reconnected (if it was previously subscribed when connected).
267 | * @param onMessage The callback called when a message or a Push Notification arrives at the channel.
268 | */
269 | - (void)subscribeWithNotifications:(NSString*) channel subscribeOnReconnected:(BOOL) aSubscribeOnReconnected onMessage:(void (^)(OrtcClient* ortc, NSString* channel, NSString* message)) onMessage;
270 |
271 | /**
272 | * Unsubscribes from a channel to stop receiving messages sent to it.
273 | *
274 | * @param channel The channel name.
275 | */
276 | - (void)unsubscribe:(NSString*) channel;
277 | /**
278 | * Disconnects.
279 | */
280 | - (void)disconnect;
281 | /**
282 | * Indicates whether is subscribed to a channel or not.
283 | *
284 | * @param channel The channel name.
285 | *
286 | * @return TRUE if subscribed to the channel or FALSE if not.
287 | */
288 | - (NSNumber*)isSubscribed:(NSString*) channel;
289 |
290 | /** Saves the channels and its permissions for the authentication token in the ORTC server.
291 | @warning This function will send your private key over the internet. Make sure to use secure connection.
292 | @param url ORTC server URL.
293 | @param isCluster Indicates whether the ORTC server is in a cluster.
294 | @param authenticationToken The authentication token generated by an application server (for instance: a unique session ID).
295 | @param authenticationTokenIsPrivate Indicates whether the authentication token is private (1) or not (0).
296 | @param applicationKey The application key provided together with the ORTC service purchasing.
297 | @param timeToLive The authentication token time to live (TTL), in other words, the allowed activity time (in seconds).
298 | @param privateKey The private key provided together with the ORTC service purchasing.
299 | @param permissions The channels and their permissions (w: write, r: read, p: presence, case sensitive).
300 | @return TRUE if the authentication was successful or FALSE if it was not.
301 | */
302 | - (BOOL)saveAuthentication:(NSString*) url isCLuster:(BOOL) isCluster authenticationToken:(NSString*) authenticationToken authenticationTokenIsPrivate:(BOOL) authenticationTokenIsPrivate applicationKey:(NSString*) applicationKey timeToLive:(int) timeToLive privateKey:(NSString*) privateKey permissions:(NSMutableDictionary*) permissions;
303 |
304 | /** Enables presence for the specified channel with first 100 unique metadata if true.
305 |
306 | @warning This function will send your private key over the internet. Make sure to use secure connection.
307 | @param url Server containing the presence service.
308 | @param isCluster Specifies if url is cluster.
309 | @param applicationKey Application key with access to presence service.
310 | @param privateKey The private key provided when the ORTC service is purchased.
311 | @param channel Channel with presence data active.
312 | @param metadata Defines if to collect first 100 unique metadata.
313 | @param callback Callback with error (NSError) and result (NSString) parameters
314 | */
315 | - (void)enablePresence:(NSString*) aUrl isCLuster:(BOOL) aIsCluster applicationKey:(NSString*) aApplicationKey privateKey:(NSString*) aPrivateKey channel:(NSString*) channel metadata:(BOOL) aMetadata callback:(void (^)(NSError* error, NSString* result)) aCallback;
316 |
317 | /** Disables presence for the specified channel.
318 |
319 | @warning This function will send your private key over the internet. Make sure to use secure connection.
320 | @param url Server containing the presence service.
321 | @param isCluster Specifies if url is cluster.
322 | @param applicationKey Application key with access to presence service.
323 | @param privateKey The private key provided when the ORTC service is purchased.
324 | @param channel Channel with presence data active.
325 | @param callback Callback with error (NSError) and result (NSString) parameters
326 | */
327 | - (void)disablePresence:(NSString*) aUrl isCLuster:(BOOL) aIsCluster applicationKey:(NSString*) aApplicationKey privateKey:(NSString*) aPrivateKey channel:(NSString*) channel callback:(void (^)(NSError* error, NSString* result)) aCallback;
328 |
329 | /**
330 | * Gets a NSDictionary indicating the subscriptions in the specified channel and if active the first 100 unique metadata.
331 | *
332 | * @param url Server containing the presence service.
333 | * @param isCluster Specifies if url is cluster.
334 | * @param applicationKey Application key with access to presence service.
335 | * @param authenticationToken Authentication token with access to presence service.
336 | * @param channel Channel with presence data active.
337 | * @param callback Callback with error (NSError) and result (NSDictionary) parameters
338 | */
339 | - (void)presence:(NSString*) aUrl isCLuster:(BOOL) aIsCluster applicationKey:(NSString*) aApplicationKey authenticationToken:(NSString*) aAuthenticationToken channel:(NSString*) channel callback:(void (^)(NSError* error, NSDictionary* result)) aCallback;
340 |
341 | /**
342 | * Get heartbeat interval.
343 | */
344 | - (int) getHeartbeatTime;
345 | /**
346 | * Set heartbeat interval.
347 | */
348 | - (void) setHeartbeatTime:(int) newHeartbeatTime;
349 | /**
350 | * Get how many times can the client fail the heartbeat.
351 | */
352 | - (int) getHeartbeatFails;
353 | /**
354 | * Set heartbeat fails. Defines how many times can the client fail the heartbeat.
355 | */
356 | - (void) setHeartbeatFails:(int) newHeartbeatFails;
357 | /**
358 | * Indicates whether heartbeat is active or not.
359 | */
360 | - (BOOL) isHeartbeatActive;
361 | /**
362 | * Enables the client heartbeat
363 | */
364 | - (void) enableHeartbeat;
365 | /**
366 | * Disables the client heartbeat
367 | */
368 | - (void) disableHeartbeat;
369 |
370 |
371 | + (void) setDEVICE_TOKEN:(NSString *) deviceToken;
372 | @end
373 |
374 |
375 | #endif
376 |
377 |
--------------------------------------------------------------------------------
/www/OrtcPlugin.js:
--------------------------------------------------------------------------------
1 | (function(cordova) {
2 |
3 | function OrtcPushPlugin() {}
4 |
5 | OrtcPushPlugin.prototype.checkForNotifications = function() {
6 | var promise = new Promise(function(resolve, reject) {
7 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "checkForNotifications", []);
8 | });
9 | return promise;
10 | };
11 |
12 | OrtcPushPlugin.prototype.removeNotifications = function() {
13 | var promise = new Promise(function(resolve, reject) {
14 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "removeNotifications", []);
15 | });
16 | return promise;
17 | };
18 |
19 | OrtcPushPlugin.prototype.connect = function(config) {
20 | var promise = new Promise(function(resolve, reject) {
21 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "connect", config ? [config] : []);
22 | });
23 | return promise;
24 | };
25 |
26 | OrtcPushPlugin.prototype.getIsConnected = function() {
27 | var promise = new Promise(function(resolve, reject) {
28 | cordova.exec(function(res){resolve(res);}, function(){reject();}, "OrtcPushPlugin", "getIsConnected", []);
29 | });
30 | return promise;
31 | };
32 |
33 | OrtcPushPlugin.prototype.enableHeadsUpNotifications = function() {
34 | var promise = new Promise(function(resolve, reject) {
35 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "enableHeadsUpNotifications", []);
36 | });
37 | return promise;
38 | };
39 |
40 | OrtcPushPlugin.prototype.disableHeadsUpNotifications = function() {
41 | var promise = new Promise(function(resolve, reject) {
42 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "disableHeadsUpNotifications", []);
43 | });
44 | return promise;
45 | };
46 |
47 | OrtcPushPlugin.prototype.disconnect = function() {
48 | var promise = new Promise(function(resolve, reject) {
49 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "disconnect", []);
50 | });
51 | return promise;
52 | };
53 |
54 | OrtcPushPlugin.prototype.subscribe = function(config) {
55 | var promise = new Promise(function(resolve, reject) {
56 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "subscribe", config ? [config] : []);
57 | });
58 | return promise;
59 | };
60 |
61 |
62 | OrtcPushPlugin.prototype.unsubscribe = function(config) {
63 | var promise = new Promise(function(resolve, reject) {
64 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "unsubscribe", config ? [config] : []);
65 | });
66 | return promise;
67 | };
68 |
69 | OrtcPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge) {
70 | var promise = new Promise(function(resolve, reject) {
71 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "setApplicationIconBadgeNumber", [badge]);
72 | });
73 | return promise;
74 | };
75 |
76 | OrtcPushPlugin.prototype.send = function(config) {
77 | cordova.exec(null, null, "OrtcPushPlugin", "send", config ? [config] : []);
78 | };
79 |
80 | // Call this to clear all notifications from the notification center
81 | OrtcPushPlugin.prototype.cancelAllLocalNotifications = function() {
82 | var promise = new Promise(function(resolve, reject) {
83 | cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "cancelAllLocalNotifications", []);
84 | });
85 | return promise;
86 | };
87 |
88 | OrtcPushPlugin.prototype.log = function(log) {
89 | cordova.exec(null, null, "OrtcPushPlugin", "log", log ? [log] : []);
90 | };
91 |
92 | OrtcPushPlugin.prototype.receiveRemoteNotification = function(channel, payload, tapped) {
93 | var ev = document.createEvent('HTMLEvents');
94 | ev.channel = channel;
95 | ev.payload = payload;
96 | ev.tapped = tapped;
97 | ev.initEvent('push-notification', true, true, arguments);
98 | document.dispatchEvent(ev);
99 | };
100 |
101 | OrtcPushPlugin.prototype.onException = function(error){
102 | var ev = document.createEvent('HTMLEvents');
103 | ev.description = error;
104 | ev.initEvent('onException', true, true, arguments);
105 | document.dispatchEvent(ev);
106 | };
107 |
108 | cordova.addConstructor(function() {
109 | if(!window.plugins) window.plugins = {};
110 | window.plugins.OrtcPushPlugin = new OrtcPushPlugin();
111 | });
112 |
113 | })(window.cordova || window.Cordova || window.PhoneGap);
114 |
115 |
116 | // call when device is ready
117 | document.addEventListener("deviceready", function () {
118 | if(window.plugins && window.plugins.OrtcPushPlugin){
119 | var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
120 | OrtcPushPlugin.checkForNotifications();
121 |
122 | }
123 | });
124 |
125 |
126 | // call when app resumes
127 | document.addEventListener("resume", function () {
128 | if(window.plugins && window.plugins.OrtcPushPlugin){
129 | var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
130 | OrtcPushPlugin.checkForNotifications();
131 | }
132 | });
133 |
134 |
135 |
--------------------------------------------------------------------------------