我正在尝试将Firebase设备发送到设备推送通知,因此我有如下代码。
private void sendNotification(final String token) {
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... voids) {
try {
OkHttpClient client=new OkHttpClient();
JSONObject json=new JSONObject();
JSONObject dataJson=new JSONObject();
dataJson.put("body","Hello, I am body");
dataJson.put("title","I am title");
json.put("notification",dataJson);
json.put("to",token);
RequestBody body=RequestBody.create(JSON,json.toString());
Request request=new Request.Builder()
.header("Authorization","key="+LEGACY_SERVER_KEY)
.url("https://fcm.googleapis.com/fcm/send")
.post(body)
.build();
Response response=client.newCall(request).execute();
String finalResponse=response.body().string();
//Toast.makeText(MainActivity.this, finalResponse, Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MainActivity.this, "Exception occured", Toast.LENGTH_SHORT).show();
}
return null;
}
}.execute();
}
这里的问题是通知未发送到其他设备。但是当我取消注释 Toast(尝试块的最后一行(时,通知将成功发送并立即显示应用程序停止工作。有人请帮忙。
当我取消注释时,Toast(尝试块的最后一行(通知将发送,但它说不幸的是应用程序停止了。日志猫是:
01-23 07:17:11.907 4694-4731/bhavana.apps.saveme E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: bhavana.apps.saveme, PID: 4694
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
at android.widget.Toast$TN.<init>(Toast.java:390)
at android.widget.Toast.<init>(Toast.java:114)
at android.widget.Toast.makeText(Toast.java:277)
at android.widget.Toast.makeText(Toast.java:267)
at bhavana.apps.saveme.MainActivity$6.doInBackground(MainActivity.java:199)
at bhavana.apps.saveme.MainActivity$6.doInBackground(MainActivity.java:176)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
只能在 UI 线程中显示 Toast。
删除此行或将其移至onPostExecute
:
Toast.makeText(MainActivity.this, "Exception occured", Toast.LENGTH_SHORT).show();
Toast.makeText()
需要在UI Thread中执行。尝试将其替换为:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Exception occured", Toast.LENGTH_SHORT).show();
}
});
或者,如果您只需要调试,则只需使用 Log:
android.util.Log.i("debug", "success msg: " + finalResponse);
您可以通过覆盖onPostExecute()
来检查doInBackground()
的状态
@Override
protected void onPostExecute(Boolean result) {
android.util.Log.i("debug", "onPostExecute: " + result);
}
这假设onPostExecute()
返回Boolean
,因此:
@Override
protected Boolean doInBackground(Void... params) {
......
// return true on success
if (success) {
return true;
}
return false;
}
类需要这样定义:
new AsyncTask<Void, Void, Boolean>(){
由以下原因引起:java.lang.Runtime异常:无法在线程上吐司 没有调用 Looper.prepare((
您应该使用 runOnUiThread
.
在 UI 线程上运行指定的操作。如果当前线程是 UI 线程,然后立即执行操作。如果当前 线程不是 UI 线程,操作发布到事件队列 的 UI 线程。
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Exception occured", Toast.LENGTH_SHORT).show();
}
});