2017-08-04 18 views
0

OneSignalプッシュ通知で現在の「Test Message」文字列を変更しようとしています。私は自分のコードで定義された変数を使いたいだけですが、その方法を理解することはできません。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で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()); 
        } 
       }); 
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にキャストできません –