编辑 OneSignal 通知中的消息内容 Android

Edit message contents in OneSignal notification Android

我正在尝试更改 OneSignal 推送通知中的当前 'Test Message' 字符串。我只是想使用我的代码中定义的变量,但不知道该怎么做。

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

我能够在向选定用户发送通知时实现类似的效果。现在我只想更改实际消息的文本。

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

下面的解决方案对我有用。当前用户的全名连接到字符串消息“wants you follow them”。然后发送给具有特定 OneSignalID 的 selectedUser。

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