я не могу получить уведомление от моего сервера - PullRequest
0 голосов
/ 19 мая 2018

это мой класс, который получает данные с сервера: он не показывает уведомление , но если я возьму sendBroadcast (намерение) и Intent из блоков try и catch и сделаю msg = null;он показывает уведомление, но с пустым сообщением , пожалуйста, мне нужна ваша помощь.

package com.example.hussienalrubaye.myapplication;


import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



public class Sev_data extends IntentService {
    public static boolean ServiceIsRun = false;
    public Sev_data() {
        super("MyWebRequestService");
    }
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        while (ServiceIsRun) {
            int id_s=0;//هنا رقم اخر رسلة وصلة التطبيق
            String url0 = "http://localhost/sendapp.php?co>"+id_s;//رايط ملف الداتة من السيرفر
            String  msg;
            try {
                URL url = new URL(url0);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                msg = Stream2String(in);
                in.close();
                JSONObject jsonRootObject = new JSONObject(msg);
                JSONArray jsonArray = jsonRootObject.optJSONArray("date");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id= jsonObject.optString("id");
                    String title = jsonObject.optString("title");
                    String mess = jsonObject.optString("description");
                    ///// هنا نهاية الموضوع دي اخر رسلة علي السيرفر
                }
                // creat new intent
                intent = new Intent();
                //set the action that will receive our broadcast
                intent.setAction("com.example.Broadcast");
                // add data to the bundle
                intent.putExtra("msg", msg);
                // send the data to broadcast
                sendBroadcast(intent);
                //delay for 50000ms
            } catch (Exception e) {
            }
            try{
                Thread.sleep(20000);
            }catch (Exception ex){}
        }
    }
    String date="";
    public String Stream2String(InputStream inputStream) {
        BufferedReader bureader=new BufferedReader( new InputStreamReader(inputStream));
        String line ;
        String Text="";
        try{
            while((line=bureader.readLine())!=null) {
                Text+=line;
            }
            inputStream.close();
        }catch (Exception ex){}
        return Text;
    }
}

, и это класс получателя:

package com.example.doblist.mydobservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // get the bundles in the message
        final Bundle bundle = intent.getExtras();
        // check the action equal to the action we fire in broadcast,
        if   (   intent.getAction().equalsIgnoreCase("com.example.Broadcast"))
        //read the data from the intent
        { NewMessageNotification notfilyme= new NewMessageNotification();
            notfilyme.notify(context,bundle.getString("msg"),223);}
        //  Toast.makeText(context,bundle.getString("msg"),Toast.LENGTH_LONG).show();
    }
}

Я добавляю все разрешения AndroidManifest все хорошо, но этодоза не показывает уведомление Я хочу знать.это код, который получает данные с сервера, верно или нет

...