我无法从我的服务器收到任何通知

时间:2018-05-19 00:10:02

标签: android json android-notifications android-broadcastreceiver

这是我的从服务器接收数据的类:它不显示通知 但是如果我把sendBroadcast(intent)和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权限,但它没有显示通知 我想知道。是从服务器获取数据的鳕鱼是否为真

0 个答案:

没有答案