Skip to content

Commit 45ac6b0

Browse files
Merge pull request #29 from JoaoCaixinha/master
Promise based javascript interface
2 parents aed74ae + fe8905b commit 45ac6b0

File tree

4 files changed

+88
-56
lines changed

4 files changed

+88
-56
lines changed

README.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Push Notifications only work in real devices for the iOS platform (not on simula
2626
4. **Build and run the app in an iOS/Android device**
2727

2828
NOTE: iOS - The Push Notifications won't work on the simulators, only on actual devices.
29-
NOTE: Android - The Push Notifications will work on the emulators only if is a Google APIs emulator.
29+
NOTE: Android - The Push Notifications will work on the emulators only if is a Google APIs emulator.
3030

3131
* * *
3232

@@ -36,32 +36,32 @@ Push Notifications only work in real devices for the iOS platform (not on simula
3636

3737
* * *
3838

39-
* **checkForNotifications(callback())**
39+
* **var promise = checkForNotifications()**
4040

4141
This method is used to verify push notifications on buffer on the native code from the javascript interface.
4242

43-
* **removeNotifications(callback())**
43+
* **var promise = removeNotifications()**
4444

4545
This method is used clear the last push notifications on buffer on the native code from the javascript interface.
4646

47-
* callback() - is triggered after iOS/Android native finishes processing.
48-
* **connect(config, successCallback())**
47+
* promise - resolved after iOS/Android native finishes processing.
48+
* **var promise = connect(config)**
4949

5050
This method is used to establish the ORTC connection.
5151

5252
* config - is a JSONObject with the config to connect. Ex: {'appkey':'YOUR_APPLICATION_KEY', 'token':'myToken', 'metadata':'myMetadata', 'url':'https://ortc-developers.realtime.co/server/ssl/2.1/','projectId':'YOUR_GOOGLE_PROJECT_NUMBER'}. ProjectId only necessary on Android Push Notifications.
53-
* successCallback() - this function is call when connection is established.
54-
* **disconnect(callback())**
53+
* promise - resolve is call when connection is established.
54+
* **var promise = disconnect()**
5555

5656
This method is used to disconnect the ORTC connection.
57-
58-
* **getIsConnected(callback(state))**
57+
* promise - resolved after connection is disconnected.
58+
59+
* **var promise = getIsConnected()**
5960

6061
Gets ortc client connection state.
61-
Returns callback state 0 if not connected and 1 connected.
62-
63-
* callback() - is triggered after connection is disconnected.
64-
* **subscribe(channel, callback())**
62+
63+
Resolves promise with parameter state 0 if not connected and 1 connected.
64+
* **var promise = subscribe(channel)**
6565

6666
Subscribe a channel. Note: In order to receive the push notifications on the channel you just subscribed you have to add an event listener with the name "push-notification" to your html like:
6767

@@ -74,36 +74,36 @@ Push Notifications only work in real devices for the iOS platform (not on simula
7474
payload.value = JSON.stringify( notification.payload );
7575
}, false);
7676

77-
**The object notification passed as argument** on the callback function is an JSONObject with the fields **channel** and **payload**, where channel is the name of the channel and payload is the content you sent. (like a JSONObject or a String).
77+
**The object notification passed as argument** on the listener function is an JSONObject with the fields **channel** and **payload**, where channel is the name of the channel and payload is the content you sent. (like a JSONObject or a String).
7878

7979
Ex: {"channel":"mychannel","payload":"{"sound":"default","badge":"1","name":"Joe","age" :"48}"}
8080

8181
**NOTE: Please in your payload try to avoid '\n' since you can have some issues.**
8282

8383
* is a JSONObject with the channel to subscribe. Ex: {'channel':'mychannel'}
84-
* callback() - is triggered after channel is subscribed.
85-
* **unsubscribe(channel,callback())**
84+
* promise - resolved when channel is subscribed.
85+
* **var promise = unsubscribe(channel)**
8686

8787
This method is used to unsubscribe a channel previously subscribed.
8888

8989
* channel - is a JSONObject with the channel name to unsubscribe. Ex: {'channel':'mychannel'}
90-
* callback() - is triggered after channel is unsubscribed.
91-
* **setApplicationIconBadgeNumber(badge,callback())**
90+
* promise - resolved when channel is unsubscribed.
91+
* **var promise = setApplicationIconBadgeNumber(badge)**
9292

9393
This method is used to set the application badge number on iOS. Not implemented on Android.
9494

9595
* badge - the number to appear on the bage.
96-
* callback() - is triggered after iOS/Android native code finishes.
96+
* promise - resolved after iOS/Android native code finishes.
9797
* **send(config)**
9898

9999
This method is used to send a message to a channel.
100100

101101
* config - is a JSONObject with the channel to send the message. Ex: {'channel':'mychannel','message':'mymessage'}.
102-
* **cancelAllLocalNotifications(callback())**
102+
* **var promise = cancelAllLocalNotifications()**
103103

104104
This method is used to clear notifications from notification center.
105105

106-
* callback() - is triggered after iOS/Android native code finishes.
106+
* promise - resolved after iOS/Android native code finishes.
107107
* **log(logString)**
108108

109109
This is a handy method to log data into XCODE/AndroidStudio console from the javascript code.
@@ -137,9 +137,9 @@ Push Notifications only work in real devices for the iOS platform (not on simula
137137

138138
Only available for android. [Check android documentation](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up)
139139

140-
**enableHeadsUpNotifications** Use this method to set the notification display type to Heads-up. This method persists the set value, to disable the heads-up notifications you must call `disableHeadsUpNotifications`.
140+
**var promise = enableHeadsUpNotifications()** Use this method to set the notification display type to Heads-up. This method persists the set value, to disable the heads-up notifications you must call `disableHeadsUpNotifications`.
141141

142-
**disableHeadsUpNotifications** Use this method to set the default notifications display (only the small icon is shown in the notification bar) and disable Heads-up.
142+
**var promise = disableHeadsUpNotifications()** Use this method to set the default notifications display (only the small icon is shown in the notification bar) and disable Heads-up.
143143

144144
## Usage example
145145

@@ -167,11 +167,13 @@ Add to your app:
167167
var OrtcPushPlugin = window.plugins.OrtcPushPlugin;
168168
OrtcPushPlugin.log("Connecting");
169169

170-
OrtcPushPlugin.connect({'appkey':'YOUR_APPLICATION_KEY', 'token':'myToken', 'metadata':'myMetadata', 'url':'https://ortc-developers.realtime.co/server/ssl/2.1/','projectId':'YOUR_GOOGLE_PROJECT_NUMBER'}, function (){
170+
OrtcPushPlugin.connect({'appkey':'YOUR_APPLICATION_KEY', 'token':'myToken', 'metadata':'myMetadata', 'url':'https://ortc-developers.realtime.co/server/ssl/2.1/','projectId':'YOUR_GOOGLE_PROJECT_NUMBER'})
171+
.then(function (){
171172
OrtcPushPlugin.log("Connected: ");
172173
var channel = document.getElementById('channel');
173174
OrtcPushPlugin.log("Trying to subscribe: " + channel.value);
174-
OrtcPushPlugin.subscribe({'channel':channel.value}, function (){
175+
OrtcPushPlugin.subscribe({'channel':channel.value})
176+
.then(function (){
175177
var subcribed = document.getElementById('subscribed');
176178
subcribed.innerHTML = "subscribed: " + channel.value;
177179
OrtcPushPlugin.log("subscribed: " + channel.value);

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
22
"name": "cordovapush",
3-
"version": "0.1.31",
3+
"version": "0.2.0",
44
"description": "This Cordova plugin should be used with the iOS/Android platforms together with the Realtime Messaging library (ORTC) for Push Notifications support.",
55
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
96
"repository": {
107
"type": "git",
118
"url": "git+https://github.com/realtime-framework/CordovaPush.git"

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
id="co.realtime.plugins.CordovaPush"
4-
version="0.1.31">
4+
version="0.2.0">
55

66
<name>cordovapush</name>
77
<author>Reatime</author>

www/OrtcPlugin.js

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,92 @@
11
(function(cordova) {
22

33
function OrtcPushPlugin() {}
4-
5-
6-
OrtcPushPlugin.prototype.checkForNotifications = function(success) {
7-
cordova.exec(success, success, "OrtcPushPlugin", "checkForNotifications", []);
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;
810
};
911

10-
OrtcPushPlugin.prototype.removeNotifications = function(success) {
11-
cordova.exec(success, success, "OrtcPushPlugin", "removeNotifications", []);
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;
1217
};
1318

14-
OrtcPushPlugin.prototype.connect = function(config, success) {
15-
cordova.exec(success, success, "OrtcPushPlugin", "connect", config ? [config] : []);
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;
1624
};
1725

18-
OrtcPushPlugin.prototype.getIsConnected = function(success) {
19-
cordova.exec(success, success, "OrtcPushPlugin", "getIsConnected", []);
26+
OrtcPushPlugin.prototype.getIsConnected = function() {
27+
var promise = new Promise(function(resolve, reject) {
28+
cordova.exec(function(){resolve();}, function(){reject();}, "OrtcPushPlugin", "getIsConnected", []);
29+
});
30+
return promise;
2031
};
2132

22-
OrtcPushPlugin.prototype.enableHeadsUpNotifications = function(config, success) {
23-
cordova.exec(success, success, "OrtcPushPlugin", "enableHeadsUpNotifications", []);
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;
2438
};
2539

26-
OrtcPushPlugin.prototype.disableHeadsUpNotifications = function(config, success) {
27-
cordova.exec(success, success, "OrtcPushPlugin", "disableHeadsUpNotifications", []);
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;
2845
};
2946

30-
OrtcPushPlugin.prototype.disconnect = function(success) {
31-
cordova.exec(success, success, "OrtcPushPlugin", "disconnect", []);
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;
3252
};
3353

34-
OrtcPushPlugin.prototype.subscribe = function(config, success) {
35-
cordova.exec(success, success, "OrtcPushPlugin", "subscribe", config ? [config] : []);
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;
3659
};
3760

3861

39-
OrtcPushPlugin.prototype.unsubscribe = function(config, success) {
40-
cordova.exec(success, success, "OrtcPushPlugin", "unsubscribe", config ? [config] : []);
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;
4167
};
4268

43-
OrtcPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
44-
cordova.exec(callback, callback, "OrtcPushPlugin", "setApplicationIconBadgeNumber", [badge]);
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;
4574
};
4675

4776
OrtcPushPlugin.prototype.send = function(config) {
48-
cordova.exec(null, null, "OrtcPushPlugin", "send", config ? [config] : []);
77+
cordova.exec(null, null, "OrtcPushPlugin", "send", config ? [config] : []);
4978
};
5079

5180
// Call this to clear all notifications from the notification center
52-
OrtcPushPlugin.prototype.cancelAllLocalNotifications = function(callback) {
53-
cordova.exec(callback, callback, "OrtcPushPlugin", "cancelAllLocalNotifications", []);
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;
5486
};
5587

5688
OrtcPushPlugin.prototype.log = function(log) {
57-
cordova.exec(null, null, "OrtcPushPlugin", "log", log ? [log] : []);
89+
cordova.exec(null, null, "OrtcPushPlugin", "log", log ? [log] : []);
5890
};
5991

6092
OrtcPushPlugin.prototype.receiveRemoteNotification = function(channel, payload) {
@@ -98,3 +130,4 @@ document.addEventListener("resume", function () {
98130
}
99131
});
100132

133+

0 commit comments

Comments
 (0)