Skip to content

Commit 0f8bb54

Browse files
committed
Added new notification settings, setSubscription, and postNotification
* 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.
1 parent bec01f0 commit 0f8bb54

14 files changed

+501
-43
lines changed

LICENSE

+10
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,13 @@ Includes portions from DTTJailbreakDetection:
111111
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
112112
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
113113
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
114+
115+
Includes portions from BSMobileProvision:
116+
Copyright (c) 2013, The Blindsight Corporation
117+
All rights reserved.
118+
119+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
120+
121+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
122+
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.
123+
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.

plugin.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="com.onesignal.plugins.OneSignal"
5-
version="1.5.0">
5+
version="1.6.0">
66

77
<name>OneSignal Push Notifications</name>
88
<author>Josh Kasten</author>
@@ -101,13 +101,15 @@
101101
<source-file src="src/ios/OneSignalJailbreakDetection.m" />
102102
<source-file src="src/ios/OneSignalPush.m" />
103103
<source-file src="src/ios/OneSignalReachability.m" />
104-
104+
<source-file src="src/ios/OneSignalMobileProvision.m" />
105+
105106
<header-file src="src/ios/OneSignal.h" />
106107
<header-file src="src/ios/OneSignalHTTPClient.h" />
107108
<header-file src="src/ios/OneSignalTrackIAP.h" />
108109
<header-file src="src/ios/OneSignalJailbreakDetection.h" />
109110
<header-file src="src/ios/OneSignalPush.h" />
110111
<header-file src="src/ios/OneSignalReachability.h" />
112+
<header-file src="src/ios/OneSignalMobileProvision.h" />
111113

112114
</platform>
113115

src/android/com/plugin/gcm/OneSignalPush.java

+82-15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.onesignal.OneSignal.NotificationOpenedHandler;
4848
import com.onesignal.OneSignal.GetTagsHandler;
4949
import com.onesignal.OneSignal.IdsAvailableHandler;
50+
import com.onesignal.OneSignal.PostNotificationResponseHandler;
5051

5152
public class OneSignalPush extends CordovaPlugin {
5253
public static final String TAG = "OneSignalPush";
@@ -60,6 +61,10 @@ public class OneSignalPush extends CordovaPlugin {
6061
public static final String REGISTER_FOR_PUSH_NOTIFICATIONS = "registerForPushNotifications";
6162
public static final String ENABLE_VIBRATE = "enableVibrate";
6263
public static final String ENABLE_SOUND = "enableSound";
64+
public static final String ENABLE_NOTIFICATIONS_WHEN_ACTIVE = "enableNotificationsWhenActive";
65+
public static final String ENABLE_INAPP_ALERT_NOTIFICATION = "enableInAppAlertNotification";
66+
public static final String SET_SUBSCRIPTION = "setSubscription";
67+
public static final String POST_NOTIFICATION = "postNotification";
6368
public static final String SET_LOG_LEVEL = "setLogLevel";
6469

6570
// This is to prevent an issue where if two Javascript calls are made to OneSignal expecting a callback then only one would fire.
@@ -68,6 +73,12 @@ private static void callbackSuccess(CallbackContext callbackContext, JSONObject
6873
pluginResult.setKeepCallback(true);
6974
callbackContext.sendPluginResult(pluginResult);
7075
}
76+
77+
private static void callbackError(CallbackContext callbackContext, JSONObject jsonObject) {
78+
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, jsonObject);
79+
pluginResult.setKeepCallback(true);
80+
callbackContext.sendPluginResult(pluginResult);
81+
}
7182

7283
private static void callbackError(CallbackContext callbackContext, String str) {
7384
PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, str);
@@ -82,25 +93,12 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
8293
if (INIT.equals(action)) {
8394
try {
8495
JSONObject jo = data.getJSONObject(0);
85-
final CallbackContext jsNotificationOpenedCallBack = callbackContext;
96+
OneSignal.sdkType = "cordova";
8697
OneSignal.init(
8798
(Activity)this.cordova.getActivity(),
8899
jo.getString("googleProjectNumber"),
89100
jo.getString("appId"),
90-
new NotificationOpenedHandler() {
91-
@Override
92-
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
93-
JSONObject outerObject = new JSONObject();
94-
try {
95-
outerObject.put("message", message);
96-
outerObject.put("additionalData", additionalData);
97-
outerObject.put("isActive", isActive);
98-
callbackSuccess(jsNotificationOpenedCallBack, outerObject);
99-
} catch (Throwable t) {
100-
t.printStackTrace();
101-
}
102-
}
103-
});
101+
new CordovaNotificationOpenedHandler(callbackContext));
104102

105103
result = true;
106104
} catch (JSONException e) {
@@ -199,6 +197,52 @@ else if (ENABLE_SOUND.equals(action)) {
199197
t.printStackTrace();
200198
}
201199
}
200+
else if (ENABLE_NOTIFICATIONS_WHEN_ACTIVE.equals(action)) {
201+
try {
202+
OneSignal.enableNotificationsWhenActive(data.getBoolean(0));
203+
result = true;
204+
} catch (Throwable t) {
205+
t.printStackTrace();
206+
}
207+
}
208+
else if (ENABLE_INAPP_ALERT_NOTIFICATION.equals(action)) {
209+
try {
210+
OneSignal.enableInAppAlertNotification(data.getBoolean(0));
211+
result = true;
212+
} catch (Throwable t) {
213+
t.printStackTrace();
214+
}
215+
}
216+
else if (SET_SUBSCRIPTION.equals(action)) {
217+
try {
218+
OneSignal.setSubscription(data.getBoolean(0));
219+
result = true;
220+
} catch (Throwable t) {
221+
t.printStackTrace();
222+
}
223+
}
224+
else if (POST_NOTIFICATION.equals(action)) {
225+
try {
226+
JSONObject jo = data.getJSONObject(0);
227+
final CallbackContext jsPostNotificationCallBack = callbackContext;
228+
OneSignal.postNotification(jo,
229+
new PostNotificationResponseHandler() {
230+
@Override
231+
public void onSuccess(JSONObject response) {
232+
callbackSuccess(jsPostNotificationCallBack, response);
233+
}
234+
235+
@Override
236+
public void onFailure(JSONObject response) {
237+
callbackError(jsPostNotificationCallBack, response);
238+
}
239+
});
240+
241+
result = true;
242+
} catch (Throwable t) {
243+
t.printStackTrace();
244+
}
245+
}
202246
else if (SET_LOG_LEVEL.equals(action)) {
203247
try {
204248
JSONObject jo = data.getJSONObject(0);
@@ -227,4 +271,27 @@ public void onResume(boolean multitasking) {
227271
super.onResume(multitasking);
228272
OneSignal.onResumed();
229273
}
274+
275+
@OneSignal.TiedToCurrentActivity
276+
private class CordovaNotificationOpenedHandler implements NotificationOpenedHandler {
277+
278+
private CallbackContext jsNotificationOpenedCallBack;
279+
280+
public CordovaNotificationOpenedHandler(CallbackContext inCallbackContext) {
281+
jsNotificationOpenedCallBack = inCallbackContext;
282+
}
283+
284+
@Override
285+
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
286+
JSONObject outerObject = new JSONObject();
287+
try {
288+
outerObject.put("message", message);
289+
outerObject.put("additionalData", additionalData);
290+
outerObject.put("isActive", isActive);
291+
callbackSuccess(jsNotificationOpenedCallBack, outerObject);
292+
} catch (Throwable t) {
293+
t.printStackTrace();
294+
}
295+
}
296+
}
230297
}

src/android/libs/OneSignalSDK.jar

11.8 KB
Binary file not shown.

src/ios/OneSignal.h

100755100644
+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
typedef void (^OneSignalResultSuccessBlock)(NSDictionary* result);
2525
typedef void (^OneSignalFailureBlock)(NSError* error);
26-
typedef void (^OneSignalIdsAvailableBlock)(NSString* playerId, NSString* pushToken);
26+
typedef void (^OneSignalIdsAvailableBlock)(NSString* userId, NSString* pushToken);
2727
typedef void (^OneSignalHandleNotificationBlock)(NSString* message, NSDictionary* additionalData, BOOL isActive);
2828

2929
/**
@@ -94,5 +94,12 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
9494

9595
- (void)IdsAvailable:(OneSignalIdsAvailableBlock)idsAvailableBlock;
9696

97+
- (void)enableInAppAlertNotification:(BOOL)enable;
98+
- (void)setSubscription:(BOOL)enable;
99+
100+
- (void)postNotification:(NSDictionary*)jsonData;
101+
- (void)postNotification:(NSDictionary*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;
102+
- (void)postNotificationWithJsonString:(NSString*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;
103+
97104
@end
98105

0 commit comments

Comments
 (0)