diff --git a/docs/API.md b/docs/API.md index 3b2ea88cc..d6bc8b0f8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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 @@ -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 @@ -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 diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 340f27495..f24565848 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -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"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 9d12e138a..2fac93320 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -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; @@ -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"); @@ -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) { @@ -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