2017-08-04 51 views
0

我正在尝试更改OneSignal推送通知中当前的“测试消息”字符串。我只是想使用我的代码中定义的变量,但不知道如何去做。在OneSignal通知中编辑邮件内容Android

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

下面的解决方案为我工作。当前用户的全名连接到字符串消息“希望你关注他们”。然后通过特定的OneSignalID发送给选定的用户。

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

使用此

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 +"\"" 
 

 

 
            + "}";

for second method follow this link

+0

当我尝试第一种方法时,我得到了这个。 java.lang.ClassCastException:org.json.JSONArray不能转换为com.onesignal.OneSignal $ PostNotificationResponseHandler –