我正在开发一个应用程序,在该应用程序上,每当用户使用代码通知时,都应推送给管理员,我已进行编程,并且已使用onesignal Webview将所有管理员订阅为活动用户,但我可以发送通知,但是当我尝试发送时,它不起作用使用编码 根据onesignal文档使用的代码,请在这里帮助我或告诉我任何替代方法以获取通知
此代码中添加REST API密钥时出错 但是当playerId用于向单个用户发送通知时,此代码有效
try {
JSONObject notificationContent = new JSONObject(
"{'contents':{'en':'" +email+"\n"+ message +" at "+currentDateTime() + "'},"+
"\"included_segments\": [\"Active Users\"],"
+
"'headings':{'en': '" + name + "'}}");
OneSignal.postNotification(notificationContent, null);
} catch (JSONException e) {
e.printStackTrace();
}
所以我尝试了此代码
try {
String jsonResponse;
URL url = new URL("https://onesignal.com/api/v1/notifications");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Authorization", "Basic YzBkYzUzN2QtZj REST API KEY 1MmY3MGVmM2Ux");
// con.setRequestProperty("Authorization", "Basic ");
con.setRequestMethod("POST");
String strJsonBody = "{"
+ "\"app_id\": \"f8b4a145-****-45b2-ab7f-49ea4*****33\","
+ "\"included_segments\": [\"Active Users\"],"
+ "\"data\": {\"foo\": \"bar\"},"
+ "\"contents\": {\"en\": \"Test Notification\"}"
+ "}";
System.out.println("strJsonBody:\n" + strJsonBody);
byte[] sendBytes = strJsonBody.getBytes("UTF-8");
con.setFixedLengthStreamingMode(sendBytes.length);
OutputStream outputStream = con.getOutputStream();
outputStream.write(sendBytes);
int httpResponse = con.getResponseCode();
System.out.println("httpResponse: " + httpResponse);
if (httpResponse >= HttpURLConnection.HTTP_OK
&& httpResponse < HttpURLConnection.HTTP_BAD_REQUEST) {
Scanner scanner = new Scanner(con.getInputStream(), "UTF-8");
jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
scanner.close();
} else {
Scanner scanner = new Scanner(con.getErrorStream(), "UTF-8");
jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
scanner.close();
}
System.out.println("jsonResponse:\n" + jsonResponse);
} catch (Throwable t) {
t.printStackTrace();
}
显示此错误
09-14 23:13:43.370 28564-28564/com.example.simpleapplocker I/System.out: strJsonBody:
09-14 23:13:43.370 28564-28564/com.example.simpleapplocker I/System.out: {"app_id": "f8b4a145-1d54-45b2-ab7f-49ea443d5433","included_segments": ["Active Users"],"data": {"foo": "bar"},"contents": {"en": "English Message"}}
09-14 23:13:43.372 28564-28564/com.example.simpleapplocker W/System.err: android.os.NetworkOnMainThreadException
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:688)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.okio.Okio$2.read(Okio.java:135)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.okio.RealBufferedSource.exhausted(RealBufferedSource.java:60)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.http.HttpConnection.isReadable(HttpConnection.java:155)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.Connection.isReadable(Connection.java:235)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.OkHttpClient$1.isReadable(OkHttpClient.java:91)
09-14 23:13:43.424 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:351)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:331)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at com.example.simpleapplocker.MainActivity$3.onClick(MainActivity.java:157)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at android.view.View.performClick(View.java:5231)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at android.view.View$PerformClick.run(View.java:21240)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
09-14 23:13:43.425 28564-28564/com.example.simpleapplocker W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
这里 MainActivity.java:157是
OutputStream outputStream = con.getOutputStream();