Skip to content

Commit

Permalink
Added new notification settings, setSubscription, and postNotification
Browse files Browse the repository at this point in the history
* Calling enableInAppAlertNotification with true will enable notifications to be displayed as alerts when one is received when the user is in your app.
   - On Android you can call enableNotificationsWhenActive with true to place notifications in the notification area instead.
* Calling setSubscription with false will unsubscribe the user from receiving push notifications.
* postNotification lets you create user to user notifications and schedule future notifications without have to make calls to the REST API yourself.
* Added more logging to help debug issues.
* Android devices now retry to subscribe to GCM for push notifications if it fails due to a connection issue.
* Fixed Android bug where the notification opened callback would stop firing after the user exited your app by pressing the back button.
  • Loading branch information
jkasten2 committed Jun 11, 2015
1 parent bec01f0 commit 0f8bb54
Show file tree
Hide file tree
Showing 14 changed files with 501 additions and 43 deletions.
10 changes: 10 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ Includes portions from DTTJailbreakDetection:
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Includes portions from BSMobileProvision:
Copyright (c) 2013, The Blindsight Corporation
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 4 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.onesignal.plugins.OneSignal"
version="1.5.0">
version="1.6.0">

<name>OneSignal Push Notifications</name>
<author>Josh Kasten</author>
Expand Down Expand Up @@ -101,13 +101,15 @@
<source-file src="src/ios/OneSignalJailbreakDetection.m" />
<source-file src="src/ios/OneSignalPush.m" />
<source-file src="src/ios/OneSignalReachability.m" />

<source-file src="src/ios/OneSignalMobileProvision.m" />

<header-file src="src/ios/OneSignal.h" />
<header-file src="src/ios/OneSignalHTTPClient.h" />
<header-file src="src/ios/OneSignalTrackIAP.h" />
<header-file src="src/ios/OneSignalJailbreakDetection.h" />
<header-file src="src/ios/OneSignalPush.h" />
<header-file src="src/ios/OneSignalReachability.h" />
<header-file src="src/ios/OneSignalMobileProvision.h" />

</platform>

Expand Down
97 changes: 82 additions & 15 deletions src/android/com/plugin/gcm/OneSignalPush.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.onesignal.OneSignal.NotificationOpenedHandler;
import com.onesignal.OneSignal.GetTagsHandler;
import com.onesignal.OneSignal.IdsAvailableHandler;
import com.onesignal.OneSignal.PostNotificationResponseHandler;

public class OneSignalPush extends CordovaPlugin {
public static final String TAG = "OneSignalPush";
Expand All @@ -60,6 +61,10 @@ public class OneSignalPush extends CordovaPlugin {
public static final String REGISTER_FOR_PUSH_NOTIFICATIONS = "registerForPushNotifications";
public static final String ENABLE_VIBRATE = "enableVibrate";
public static final String ENABLE_SOUND = "enableSound";
public static final String ENABLE_NOTIFICATIONS_WHEN_ACTIVE = "enableNotificationsWhenActive";
public static final String ENABLE_INAPP_ALERT_NOTIFICATION = "enableInAppAlertNotification";
public static final String SET_SUBSCRIPTION = "setSubscription";
public static final String POST_NOTIFICATION = "postNotification";
public static final String SET_LOG_LEVEL = "setLogLevel";

// This is to prevent an issue where if two Javascript calls are made to OneSignal expecting a callback then only one would fire.
Expand All @@ -68,6 +73,12 @@ private static void callbackSuccess(CallbackContext callbackContext, JSONObject
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

private static void callbackError(CallbackContext callbackContext, JSONObject jsonObject) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, jsonObject);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}

private static void callbackError(CallbackContext callbackContext, String str) {
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, str);
Expand All @@ -82,25 +93,12 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
if (INIT.equals(action)) {
try {
JSONObject jo = data.getJSONObject(0);
final CallbackContext jsNotificationOpenedCallBack = callbackContext;
OneSignal.sdkType = "cordova";
OneSignal.init(
(Activity)this.cordova.getActivity(),
jo.getString("googleProjectNumber"),
jo.getString("appId"),
new NotificationOpenedHandler() {
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
JSONObject outerObject = new JSONObject();
try {
outerObject.put("message", message);
outerObject.put("additionalData", additionalData);
outerObject.put("isActive", isActive);
callbackSuccess(jsNotificationOpenedCallBack, outerObject);
} catch (Throwable t) {
t.printStackTrace();
}
}
});
new CordovaNotificationOpenedHandler(callbackContext));

result = true;
} catch (JSONException e) {
Expand Down Expand Up @@ -199,6 +197,52 @@ else if (ENABLE_SOUND.equals(action)) {
t.printStackTrace();
}
}
else if (ENABLE_NOTIFICATIONS_WHEN_ACTIVE.equals(action)) {
try {
OneSignal.enableNotificationsWhenActive(data.getBoolean(0));
result = true;
} catch (Throwable t) {
t.printStackTrace();
}
}
else if (ENABLE_INAPP_ALERT_NOTIFICATION.equals(action)) {
try {
OneSignal.enableInAppAlertNotification(data.getBoolean(0));
result = true;
} catch (Throwable t) {
t.printStackTrace();
}
}
else if (SET_SUBSCRIPTION.equals(action)) {
try {
OneSignal.setSubscription(data.getBoolean(0));
result = true;
} catch (Throwable t) {
t.printStackTrace();
}
}
else if (POST_NOTIFICATION.equals(action)) {
try {
JSONObject jo = data.getJSONObject(0);
final CallbackContext jsPostNotificationCallBack = callbackContext;
OneSignal.postNotification(jo,
new PostNotificationResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
callbackSuccess(jsPostNotificationCallBack, response);
}

@Override
public void onFailure(JSONObject response) {
callbackError(jsPostNotificationCallBack, response);
}
});

result = true;
} catch (Throwable t) {
t.printStackTrace();
}
}
else if (SET_LOG_LEVEL.equals(action)) {
try {
JSONObject jo = data.getJSONObject(0);
Expand Down Expand Up @@ -227,4 +271,27 @@ public void onResume(boolean multitasking) {
super.onResume(multitasking);
OneSignal.onResumed();
}

@OneSignal.TiedToCurrentActivity
private class CordovaNotificationOpenedHandler implements NotificationOpenedHandler {

private CallbackContext jsNotificationOpenedCallBack;

public CordovaNotificationOpenedHandler(CallbackContext inCallbackContext) {
jsNotificationOpenedCallBack = inCallbackContext;
}

@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
JSONObject outerObject = new JSONObject();
try {
outerObject.put("message", message);
outerObject.put("additionalData", additionalData);
outerObject.put("isActive", isActive);
callbackSuccess(jsNotificationOpenedCallBack, outerObject);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
Binary file modified src/android/libs/OneSignalSDK.jar
Binary file not shown.
9 changes: 8 additions & 1 deletion src/ios/OneSignal.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

typedef void (^OneSignalResultSuccessBlock)(NSDictionary* result);
typedef void (^OneSignalFailureBlock)(NSError* error);
typedef void (^OneSignalIdsAvailableBlock)(NSString* playerId, NSString* pushToken);
typedef void (^OneSignalIdsAvailableBlock)(NSString* userId, NSString* pushToken);
typedef void (^OneSignalHandleNotificationBlock)(NSString* message, NSDictionary* additionalData, BOOL isActive);

/**
Expand Down Expand Up @@ -94,5 +94,12 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {

- (void)IdsAvailable:(OneSignalIdsAvailableBlock)idsAvailableBlock;

- (void)enableInAppAlertNotification:(BOOL)enable;
- (void)setSubscription:(BOOL)enable;

- (void)postNotification:(NSDictionary*)jsonData;
- (void)postNotification:(NSDictionary*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;
- (void)postNotificationWithJsonString:(NSString*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;

@end

Loading

0 comments on commit 0f8bb54

Please sign in to comment.