我正在尝试更改当前的测试消息' OneSignal推送通知中的字符串。我只是想使用我的代码中定义的变量,但无法弄清楚如何做到这一点。
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();
}
我能够在向选定用户发送通知时实现类似的功能。现在我只想更改实际消息的文本。
答案 0 :(得分:1)
使用此
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();
}
或者您可以尝试这种方式
String strJsonBody = "{"
+ " \"app_id\": \"ef42157d-64e7-4ce2-9ab7-15db224f441b\","
+ " \"included_segments\": [\"All\"],"
+ " \"data\": {\"foo\": \"bar\"},"
+ " \"contents\": {\"en\": \""+ description +"\"},"
+ " \"headings\": {\"en\": \""+ title +"\"},"
+ " \"big_picture\":\""+ imageurl +"\""
+ "}";
答案 1 :(得分:0)
以下解决方案对我有用。当前用户的全名连接到字符串消息"希望你跟随他们。"然后使用特定的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());
}
});