Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneSignal postNotification Android

I need postNotification in my native app android.

I have this code but it doesn't work:

try {
    OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}"), null);
} catch (JSONException e) {
     e.printStackTrace();
}
like image 703
user3656460 Avatar asked Aug 24 '26 07:08

user3656460


1 Answers

Can you make sure the value in userId is a valid OneSignal id on your account and it is subscribed?

You can also use the following code instead to add logcat logging to debug the issue.

try {
  OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + "userId" + "']}"),
     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 e) {
  e.printStackTrace();
}
like image 86
jkasten Avatar answered Aug 25 '26 22:08

jkasten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!