3

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();
}
4

2 回答 2

3

您能否确保其中的值userId是您帐户上的有效 OneSignal id 并且已订阅?

您也可以改用以下代码来添加 logcat 日志记录以调试问题。

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();
}
于 2016-04-28T18:38:09.240 回答
0

这对我有用,检查是否创建了 id 然后发布通知

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                Log.d("debug", "UserId:" + userId);
                if (registrationId != null) {
                    String msg_welcome  = getResources().getString(R.string.msg_welcome);
                    Log.d("debug", "registrationId:" + registrationId);
                    try {
                        OneSignal.postNotification(new JSONObject("{'contents': {'en': '"+ msg_welcome +"'}, '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();
                    }
                }
            }
        });
于 2017-09-09T15:48:51.547 回答