3333import com .onesignal .OneSignal .EmailUpdateError ;
3434
3535import org .json .JSONObject ;
36+ import org .json .JSONArray ;
3637import org .json .JSONException ;
3738
3839
@@ -95,6 +96,10 @@ private void sendEvent(String eventName, Object params) {
9596 .emit (eventName , params );
9697 }
9798
99+ private JSONObject jsonFromErrorMessageString (String errorMessage ) throws JSONException {
100+ return new JSONObject ().put ("error" , errorMessage );
101+ }
102+
98103 @ ReactMethod
99104 public void init (String appId ) {
100105 Activity activity = getCurrentActivity ();
@@ -147,8 +152,7 @@ public void onSuccess() {
147152 @ Override
148153 public void onFailure (EmailUpdateError error ) {
149154 try {
150- JSONObject errorObject = new JSONObject ("{'error' : '" + error .getMessage () + "'}" );
151- callback .invoke (RNUtils .jsonToWritableMap (errorObject ));
155+ callback .invoke (RNUtils .jsonToWritableMap (jsonFromErrorMessageString (error .getMessage ())));
152156 } catch (JSONException exception ) {
153157 exception .printStackTrace ();
154158 }
@@ -167,8 +171,7 @@ public void onSuccess() {
167171 @ Override
168172 public void onFailure (EmailUpdateError error ) {
169173 try {
170- JSONObject errorObject = new JSONObject ("{'error' : '" + error .getMessage () + "'}" );
171- callback .invoke (RNUtils .jsonToWritableMap (errorObject ));
174+ callback .invoke (RNUtils .jsonToWritableMap (jsonFromErrorMessageString (error .getMessage ())));
172175 } catch (JSONException exception ) {
173176 exception .printStackTrace ();
174177 }
@@ -187,8 +190,7 @@ public void onSuccess() {
187190 @ Override
188191 public void onFailure (EmailUpdateError error ) {
189192 try {
190- JSONObject errorObject = new JSONObject ("{'error' : '" + error .getMessage () + "'}" );
191- callback .invoke (RNUtils .jsonToWritableMap (errorObject ));
193+ callback .invoke (RNUtils .jsonToWritableMap (jsonFromErrorMessageString (error .getMessage ())));
192194 } catch (JSONException exception ) {
193195 exception .printStackTrace ();
194196 }
@@ -231,15 +233,15 @@ public void getPermissionSubscriptionState(final Callback callback) {
231233 boolean userSubscriptionEnabled = subscriptionState .getUserSubscriptionSetting ();
232234
233235 try {
234- JSONObject result = new JSONObject ("{" +
235- "'notificationsEnabled': " + String . valueOf ( notificationsEnabled ) + "," +
236- "'subscriptionEnabled': " + String .valueOf (subscriptionEnabled ) + "," +
237- "'userSubscriptionEnabled': " + String .valueOf (userSubscriptionEnabled ) + "," +
238- "'pushToken': " + subscriptionState . getPushToken () + "," +
239- "'userId': " + subscriptionState . getUserId () + "," +
240- "'emailUserId' : " + emailSubscriptionState . getEmailUserId () + "," +
241- "'emailAddress' : " + emailSubscriptionState .getEmailAddress () +
242- "}" );
236+ JSONObject result = new JSONObject ();
237+
238+ result . put ( "notificationsEnabled" , String .valueOf (notificationsEnabled ))
239+ . put ( "subscriptionEnabled" , String .valueOf (subscriptionEnabled ))
240+ . put ( "userSubscriptionEnabled" , String . valueOf ( userSubscriptionEnabled ))
241+ . put ( "pushToken" , subscriptionState . getPushToken ())
242+ . put ( "userId" , subscriptionState . getUserId ())
243+ . put ( "emailUserId" , emailSubscriptionState .getEmailUserId ())
244+ . put ( "emailAddress" , emailSubscriptionState . getEmailAddress () );
243245
244246 Log .d ("onesignal" , "permission subscription state: " + result .toString ());
245247
@@ -297,7 +299,21 @@ public void setLocationShared(Boolean shared) {
297299 @ ReactMethod
298300 public void postNotification (String contents , String data , String playerId , String otherParameters ) {
299301 try {
300- JSONObject postNotification = new JSONObject ("{'contents': " + contents + ", 'data': {'p2p_notification': " + data + "}, 'include_player_ids': ['" + playerId + "']}" );
302+ JSONObject postNotification = new JSONObject ();
303+ postNotification .put ("contents" , contents );
304+
305+ if (playerId != null ) {
306+ JSONArray playerIds = new JSONArray ();
307+ playerIds .put (playerId );
308+ postNotification .put ("include_player_ids" , playerIds );
309+ }
310+
311+ if (data != null ) {
312+ JSONObject additionalData = new JSONObject ();
313+ additionalData .put ("p2p_notification" , data );
314+ postNotification .put ("data" , additionalData );
315+ }
316+
301317 if (otherParameters != null && !otherParameters .trim ().isEmpty ()) {
302318 JSONObject parametersJson = new JSONObject (otherParameters .trim ());
303319 Iterator <String > keys = parametersJson .keys ();
0 commit comments