使用 OneSignal android 发送 PushNotifications 时出错

Error with sending PushNotifications with OneSignal android

我正在开发消息 Android 应用程序。应通过推送通知通知用户。

当时我有这个代码:

public void notifyUser(String user_id, String message) {
        try {
            OneSignal.postNotification(new JSONObject("{'contents': {'en':['" + message + "']}, " +
                            "'include_player_ids': ['" + user_id + "'], " +
                            "'headings': {'en': 'Tag sub Title HI {{user_name}}'}, " +
                            "'data': {'openURL': 'https://imgur.com'}," +
                            "'buttons':[{'id': 'id1', 'text': 'Go to GreenActivity'}, {'id':'id2', 'text': 'Go to MainActivity'}]}"),
                    new OneSignal.PostNotificationResponseHandler() {
                        @Override
                        public void onSuccess(JSONObject response) {
                            Log.d("LOL", "postNotification Success: " + response.toString());
                        }

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

但我在日志中收到错误消息:

D/LOL: postNotification Failure: {"errors":["Notification contents for each language must be a string"]}  

我在网上进行了搜索,但没有找到解决我的问题的方法。 我希望你能帮帮我... 提前致谢。

我认为您看到的错误是因为您在数组中发送消息,而它应该只是一个字符串。

"{'contents': {'en':['" + message + "']} 应该是 "{'contents': {'en':'" + message + "'}(去掉括号)。