Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #79: Subscribe to Topics
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Dec 14, 2015
1 parent 08d091d commit 1d32873
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
11 changes: 8 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Attribute | Type | Default | Description
`android.vibrate` | `boolean` | `true` | Optional. If `true` the device vibrates on receipt of notification.
`android.clearNotifications` | `boolean` | `true` | Optional. If `true` the app clears all pending notifications when it is closed.
`android.forceShow` | `boolean` | `false` | Optional. If `true` will always show a notification, even when the app is on the foreground.
`android.topics` | `array` | `[]` | Optional. If the array contains one or more strings each string will be used to subscribe to a GcmPubSub topic.

#### iOS

Expand All @@ -49,8 +50,9 @@ Attribute | Type | Default | Description
`ios.badge` | `boolean` | `false` | Optional. If `true` the device sets the badge number on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
`ios.sound` | `boolean` | `false` | Optional. If `true` the device plays a sound on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
`ios.clearBadge` | `boolean` | `false` | Optional. If `true` the badge will be cleared on app startup.
`options.ios.senderID` | `string` | `undefined` (Native) | Maps to the project number in the Google Developer Console. Setting this uses GCM for notifications instead of native
`options.ios.gcmSandbox` | `boolean` | `false` | Whether to use prod or sandbox GCM setting. Defaults to false.
`ios.senderID` | `string` | `undefined` (Native) | Maps to the project number in the Google Developer Console. Setting this uses GCM for notifications instead of native
`ios.gcmSandbox` | `boolean` | `false` | Whether to use prod or sandbox GCM setting. Defaults to false.
`ios.topics` | `array` | `[]` | Optional. If the array contains one or more strings each string will be used to subscribe to a GcmPubSub topic. Note: only usable in conjunction with `senderID`.

### Example

Expand Down Expand Up @@ -166,16 +168,19 @@ push.off('notification', callback);

**WARNING**: As stated in the example, you will have to store your event handler if you are planning to remove it.

## push.unregister(successHandler, errorHandler)
## push.unregister(successHandler, errorHandler, topics)

The unregister method is used when the application no longer wants to receive push notifications. Beware that this cleans up all event handlers previously registered, so you will need to re-register them if you want them to function again without an application reload.

If you provide a list of topics as an optional parameter then the application will unsubscribe from these topics but continue to receive other push messages.

### Parameters

Parameter | Type | Default | Description
--------- | ---- | ------- | -----------
`successHandler` | `Function` | | Is called when the api successfully unregisters.
`errorHandler` | `Function` | | Is called when the api encounters an error while unregistering.
`topics` | `Array` | | A list of topics to unsubscribe from.

### Example

Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public interface PushConstants {
public static final String FORCE_SHOW = "forceShow";
public static final String GCM = "GCM";
public static final String CONTENT_AVAILABLE = "content-available";
public static final String TOPICS = "topics";
}
68 changes: 56 additions & 12 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.iid.InstanceID;

import org.apache.cordova.CallbackContext;
Expand Down Expand Up @@ -85,6 +86,9 @@ else if (!savedSenderID.equals(senderID)) {

Log.v(LOG_TAG, "onRegistered: " + json.toString());

JSONArray topics = jo.optJSONArray(TOPICS);
subscribeToTopics(topics, token);

PushPlugin.sendEvent( json );
} else {
callbackContext.error("Empty registration ID received from GCM");
Expand Down Expand Up @@ -130,19 +134,25 @@ else if (!savedSenderID.equals(senderID)) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
Log.v(LOG_TAG, "UNREGISTER");

// Remove shared prefs
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(SOUND);
editor.remove(VIBRATE);
editor.remove(CLEAR_NOTIFICATIONS);
editor.remove(FORCE_SHOW);
editor.remove(SENDER_ID);
editor.remove(REGISTRATION_ID);
editor.commit();
String token = sharedPref.getString(REGISTRATION_ID, "");
JSONArray topics = data.optJSONArray(0);
if (topics != null && !"".equals(token)) {
unsubscribeFromTopics(topics, token);
} else {
InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
Log.v(LOG_TAG, "UNREGISTER");

// Remove shared prefs
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(SOUND);
editor.remove(VIBRATE);
editor.remove(CLEAR_NOTIFICATIONS);
editor.remove(FORCE_SHOW);
editor.remove(SENDER_ID);
editor.remove(REGISTRATION_ID);
editor.commit();
}

callbackContext.success();
} catch (IOException e) {
Expand Down Expand Up @@ -240,6 +250,40 @@ public void onDestroy() {
gWebView = null;
}

private void subscribeToTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
for (int i=0; i<topics.length(); i++) {
try {
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Subscribing to topic: " + topic);
GcmPubSub.getInstance(getApplicationContext()).subscribe(registrationToken, "/topics/" + topic, null);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to subscribe to topic: " + topic, e);
}
}
}
}

private void unsubscribeFromTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
for (int i=0; i<topics.length(); i++) {
try {
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Unsubscribing to topic: " + topic);
GcmPubSub.getInstance(getApplicationContext()).unsubscribe(registrationToken, "/topics/" + topic);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to unsubscribe to topic: " + topic, e);
}
}
}
}

/*
* serializes a bundle to JSON.
*/
Expand Down

0 comments on commit 1d32873

Please sign in to comment.