0

I am trying to change the current 'Test Message' String in a OneSignal push notification. I simply want to use a variable defined in my code, but cannot figure out how to do it.

try {
    OneSignal.postNotification(new JSONObject("{'contents': ['en': 'Test Message'], 'include_player_ids': ['" + selectedUser.getOneSignalId() + "']}"),
    new OneSignal.PostNotificationResponseHandler() {
        @Override
        public void onSuccess(JSONObject response) {
            Log.i("OneSignalExample", "postNotification Success: " + response.toString());
        }

        @Override
        public void onFailure(JSONObject response) {
            Log.e("OneSignalExample", "postNotification Failure: " + response.toString());
        }
    });
} catch (JSONException f) {
    e.printStackTrace();
}

I was able to achieve something similar in sending the notification to a selected user. Now I just want to change the text of the actual message.

Hara
  • 1,467
  • 4
  • 18
  • 35

2 Answers2

1

Use this

String yourVaribale = " what ever you want to send"

OneSignal.postNotification(new JSONObject("{'contents': ['en': " +  yourVariable + "], 'include_player_ids': ['" + selectedUser.getOneSignalId() + "']}"),
                                                            new OneSignal.PostNotificationResponseHandler() {
                                                                @Override
                                                                public void onSuccess(JSONObject response) {
                                                                    Log.i("OneSignalExample", "postNotification Success: " + response.toString());
                                                                }

                                                                @Override
                                                                public void onFailure(JSONObject response) {
                                                                    Log.e("OneSignalExample", "postNotification Failure: " + response.toString());
                                                                }
                                                            });
                                                } catch (JSONException f) {
                                                    e.printStackTrace();
                                                }

or you can try this way

String strJsonBody = "{"
                                                       +   "     \"app_id\": \"ef42157d-64e7-4ce2-9ab7-15db224f441b\","
                                                       +   "     \"included_segments\": [\"All\"],"
                                                       +   "     \"data\": {\"foo\": \"bar\"},"
                                                       +   "     \"contents\": {\"en\": \""+ description +"\"},"
                                                       +   "     \"headings\": {\"en\": \""+ title +"\"},"
                                                       +   "     \"big_picture\":\""+ imageurl +"\""


                                   + "}";

for second method follow this link

akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
  • When I try the first approach I get this. java.lang.ClassCastException: org.json.JSONArray cannot be cast to com.onesignal.OneSignal$PostNotificationResponseHandler – Dylan Castanhinha Aug 04 '17 at 14:10
0

The solution below worked for me. The full name of the current user is concatenated to the string message " wants you to follow them." and is then sent to the selectedUser with the specific OneSignalID.

    OneSignal.postNotification(new JSONObject("{'contents': {'en': \""+ currentUser.getFullName() +" wants you to follow them." +"\"}, 'include_player_ids': ['" + selectedUser.getOneSignalId() + "']}"),
                new OneSignal.PostNotificationResponseHandler() {
                    @Override
                    public void onSuccess(JSONObject response) {
                        Log.i("OneSignalExample", "postNotification Success: " + response.toString());
                    }

                    @Override
                    public void onFailure(JSONObject response) {
                        Log.e("OneSignalExample", "postNotification Failure: " + response.toString());
                    }
                });