【问题标题】:OneSignal postNotification AndroidOneSignal postNotification Android
【发布时间】:2016-04-26 09:58:51
【问题描述】:
我的原生应用 android 中需要 postNotification。
我有这段代码,但它不起作用:
try {
OneSignal.postNotification(new JSONObject("{'contents': {'en':'Test Message'}, 'include_player_ids': ['" + userId + "']}"), null);
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
标签:
android
push
onesignal
【解决方案1】:
您能否确保 userId 中的值是您帐户上的有效 OneSignal id 并且已订阅?
您也可以改用以下代码添加 logcat 日志来调试问题。
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();
}
【解决方案2】:
这对我有用,
检查是否创建了 id 然后发布通知
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();
}
}
}
});