2016-04-26 13 views
3

Ich brauche postNotification in meiner nativen App Android.OneSignal postNotification Android

Ich habe diesen Code, aber es funktioniert nicht:

try { 
    OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}"), null); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

Antwort

3

können Sie den Wert in userId stellen Sie sicher, ein gültiges OneSignal ID auf Ihrem Konto ist und es abonniert ist?

Sie können auch den folgenden Code verwenden, um Logcat-Logging hinzuzufügen, um das Problem zu debuggen.

try { 
    OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + "userId" + "']}"), 
    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 e) { 
    e.printStackTrace(); 
} 
0

Das ist für mich gearbeitet, überprüfen, ob id dann schreiben Benachrichtigung

erstellt
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() { 
      @Override 
      public void idsAvailable(String userId, String registrationId) { 
       Log.d("debug", "UserId:" + userId); 
       if (registrationId != null) { 
        String msg_welcome = getResources().getString(R.string.msg_welcome); 
        Log.d("debug", "registrationId:" + registrationId); 
        try { 
         OneSignal.postNotification(new JSONObject("{'contents': {'en': '"+ msg_welcome +"'}, 'include_player_ids': ['" + userId + "']}"), 
           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 e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     });