簡體   English   中英

在AsyncTask的doInBackground中未調用FirebaseMessagingService

[英]FirebaseMessagingService is not getting called in AsyncTask's doInBackground

我已推送通知,並且我想在電話收到通知時更新領域對象,但是當我嘗試啟動此對象時:

RealmModelActiveUser actUser= realm.where(RealmModelActiveUser.class).equalTo("id",1).findFirst();
                int myid= actUser.getUser().getUser_id();
                new ServerBackgroundDownloadConversations(getApplicationContext()) {
                    @Override
                    protected void onPostExecute(String result) {
                        super.onPostExecute(result);

                        if (!result.equals("Error")) {
                            Log.i("Conversation", "UPDATED");
                        }
                    }
                }.execute(myid);

該程序跳入構造函數ServerBackgroundDownloadConversations(getApplicationContext())但不調用doInBackground,我也不知道為什么。

我的AsyncTask:

public class ServerBackgroundCreateConversation extends AsyncTask<RealmModelConversations,Void,String> {
    Context context;

    Handler handler;
    String out= "";
    @SuppressLint("HandlerLeak")
    public ServerBackgroundCreateConversation(Context context) {
        this.context = context;

        handler =  new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle bundle= msg.getData();
                if (bundle!=null){
                    out = (String) bundle.get("response");
                } else {
                    out= "Error";
                }
            }

        };
    }


    @Override
    protected String doInBackground(RealmModelConversations... params) {
        RealmModelConversations newConv = params[0];
            UploadImageApacheHttp uploadTask = new UploadImageApacheHttp();
            uploadTask.doFileUpload(newConv.getWork(newConv.getIntWork()), newConv, handler);


            while (out.equals("")){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        return out;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(String result) {

        if (!result.equals("]") || !result.equals("")){
            /// prihlási nového user aj do active (login/register)
            CreateNewConversation(result);
        } else {
            result="Error";
        }

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


    private void CreateNewConversation(String result){
        Realm realm= Realm.getDefaultInstance();
        try {
            Gson gson = new Gson();
            Type typeConv = new TypeToken<JsonSablonaConversations>() {
            }.getType();

            JSONObject pom;
            JSONArray parentArray = new JSONArray(result);
            JSONObject finalObject = parentArray.getJSONObject(0);

            JsonSablonaConversations conversation = gson.fromJson(finalObject.toString(), typeConv);
            final RealmModelConversations NewUserConv = new RealmModelConversations();
            NewUserConv.setId_dialog(conversation.getId_dialog());
            NewUserConv.setDate(conversation.getDate());
            NewUserConv.setKey(conversation.getKey());
            NewUserConv.setId_user(conversation.getId_user());
            NewUserConv.setId_user2(conversation.getId_user2());
            NewUserConv.setMeno(conversation.getMeno());
            NewUserConv.setMeno2(conversation.getMeno2());
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    try {
                        realm.copyToRealmOrUpdate(NewUserConv);
                    } catch (Exception e) {
                        int pom=4;
                    }
                    RealmResults<RealmModelConversations> ru= realm.where(RealmModelConversations.class).findAll();
                }
            });
        }

        catch (Exception e) {
            int ppp=4;
            ppp++;
        }finally {
            realm.close();
        }
    }
}

我嘗試從服務調用的外部線程中調用此↑,但是我的AsyncTask具有處理程序,並且處理程序需要位於runOnUIthread和Thread中。 我無法獲得活動,因為該線程是從無法訪問活動的服務調用的。

我用這段代碼解決了我的問題

    public String postData(int myUserId) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.gallopshop.eu/OFY/getConversations.php");
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(myUserId)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseStr = EntityUtils.toString(response.getEntity());
        return responseStr;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    return null;
}

我將嘗試將此方法和此方法放入簡單的線程中,但是也許不需要,因為它在服務中。

問題-我將其放入Thread還是會影響應用程序的性能?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM