Skip to content

Commit

Permalink
Merge pull request #82 from m1ga/develop
Browse files Browse the repository at this point in the history
Subscribe to topics
  • Loading branch information
Jei authored Oct 10, 2017
2 parents 9c5287c + afc276f commit 1a300c0
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 2 deletions.
Binary file removed android/lib/google-play-services-tigoosh.jar
Binary file not shown.
Binary file added android/lib/play-services-base-11.0.4.aar
Binary file not shown.
Binary file added android/lib/play-services-basement-11.0.4.aar
Binary file not shown.
Binary file added android/lib/play-services-gcm-11.0.4.aar
Binary file not shown.
Binary file added android/lib/play-services-iid-11.0.4.aar
Binary file not shown.
Binary file added android/lib/play-services-tasks-11.0.4.aar
Binary file not shown.
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.0.7
version: 2.0.8
apiversion: 3
architectures: armeabi-v7a x86
description: ti.goosh
Expand Down
95 changes: 94 additions & 1 deletion android/src/ti/goosh/TiGooshModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public class TiGooshModule extends KrollModule {
private KrollFunction errorCallback = null;
private KrollFunction messageCallback = null;

/* Callbacks for topics */
private KrollFunction successUnsubTopicCallback = null;
private KrollFunction successTopicCallback = null;
private KrollFunction errorSubTopicCallback = null;
private KrollFunction errorTopicCallback = null;
private KrollFunction topicCallback = null;

public TiGooshModule() {
super();
module = this;
Expand Down Expand Up @@ -243,5 +250,91 @@ public void sendMessage(String data, Boolean inBackground) {
messageCallback.callAsync(getKrollObject(), e);
}

}
@Kroll.method
public void subscribe(final HashMap options) {
final String topic = (String) options.get("topic");

if (options.get("success") != null) {
successTopicCallback = (KrollFunction) options.get("success");
}
if (options.get("error") != null) {
errorTopicCallback = (KrollFunction) options.get("error");
}

if (topic == null || !topic.startsWith("/topics/")) {
Log.e(LCAT, "No or invalid topic specified, should start with /topics/");
return;
}

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
String token = getRemoteDeviceUUID();
GcmPubSub.getInstance(TiApplication.getInstance()).subscribe(token, topic, null);
if (successTopicCallback != null) {
// send success callback
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("success", true);
data.put("topic", topic);
successTopicCallback.callAsync(getKrollObject(), data);
}
} catch (Exception ex) {
// error
Log.e(LCAT, "Error " + ex.toString());
if (errorTopicCallback != null) {
// send error callback
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("success", false);
data.put("topic", topic);
data.put("error", ex.toString());
errorTopicCallback.callAsync(getKrollObject(), data);
}
}
return null;
}
}.execute();
}

@Kroll.method
public void unsubscribe(final HashMap options) {
final String topic = (String) options.get("topic");
successUnsubTopicCallback = (KrollFunction) options.get("success");
errorSubTopicCallback = (KrollFunction) options.get("error");

if (topic == null || !topic.startsWith("/topics/")) {
Log.e(LCAT, "No or invalid topic specified, should start with /topics/");
return;
}

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
String token = getRemoteDeviceUUID();
if (token != null) {
GcmPubSub.getInstance(TiApplication.getInstance()).unsubscribe(token, topic);
if (successUnsubTopicCallback != null) {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("success", true);
data.put("topic", topic);
successUnsubTopicCallback.callAsync(getKrollObject(), data);
}
} else {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("error", true);
data.put("topic", topic);
errorSubTopicCallback.callAsync(getKrollObject(), data);
Log.e(LCAT, "Cannot unsubscribe from topic " + topic);
}
} catch (Exception ex) {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("error", ex.toString());
data.put("topic", topic);
errorSubTopicCallback.callAsync(getKrollObject(), data);
}
return null;
}
}.execute();
}
}

0 comments on commit 1a300c0

Please sign in to comment.