Hello,

I currently have an app that receives news articles with a volley and is 
constantly updated. I want the app to send a notification to the user when 
a new article is posted. So far I have this code in my response listener 
but I'm not sure if it's in the right place or if it is detecting a new 
news article.

Any help would be useful as i'm new to android development! 

public class MainActivity extends AppCompatActivity {

    private List<NewsRecord> newsListData = new ArrayList<NewsRecord>();

    private GridView newsListView;

    private NewsListAdapter adapter;

    LinearLayout layout;

    List<String> stringValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout = (LinearLayout) findViewById(R.id.progressbar_view);

        GridView newsListView = (GridView) findViewById(R.id.newsFeedList);

        adapter = new NewsListAdapter(this, R.layout.adapter_news_list, 
newsListData, this);

        newsListView.setAdapter(adapter);

        newsListView.setOnItemClickListener(itemClicked);

        nextStart = 0;
        updateListData(nextStart, 20);
    }

    class Task extends AsyncTask<String, Integer, Boolean> {
        @Override
        protected void onPreExecute() {
            layout.setVisibility(View.VISIBLE);
            newsListView.setVisibility(View.GONE);
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            layout.setVisibility(View.GONE);
            newsListView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
            super.onPostExecute(result);
        }

        @Override
        protected Boolean doInBackground(String... params) {
            stringValues.add("String 1");
            stringValues.add("String 2");
            stringValues.add("String 3");
            stringValues.add("String 4");
            stringValues.add("String 5");

            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public int nextStart = 0;

    public void updateListData(int StartPoint, int count){
        String url = 
"http://www.efstratiou.info/projects/newsfeed/getList.php?start="; + StartPoint 
+ "&count=" + count;

        EDANewsApp app = EDANewsApp.getInstance();

        JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, 
errorListener);
        app.requestQueue.add(jsonRequest);

        nextStart +=count;
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
            case R.id.action_about:
                Intent intent = new Intent(this, AboutActivity.class);
                startActivity(intent);
                return true;
            case R.id.action_search:
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
    }

    AdapterView.OnItemClickListener itemClicked = new 
AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, 
long id) {

            Intent intent = new Intent(MainActivity.this, 
NewsItemActivity.class);

            intent.putExtra("newsItemId", newsListData.get(position).recordId);

            startActivity(intent);
        }
    };

    //Listeners
    Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            //we successfully received the JSONArray
            //Here we will extract the data and use it in our app

            //Clear the dataset before loading new data
          //  newsListData.clear();
            //Go through all the JSON objects
            for (int i = 0; i < response.length(); i++) {

                try {
                    //Get one JSON object
                    JSONObject jsonObj = response.getJSONObject(i);

                    //Put JSON data in a Java object
                    NewsRecord record = new NewsRecord();
                    record.recordId = jsonObj.getInt("record_id");
                    record.title = jsonObj.getString("title");
                    record.date = jsonObj.getString("date");
                    record.shortInfo = jsonObj.getString("short_info");
                    record.imageUrl = jsonObj.getString("image_url");

                    newsListData.add(record);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            adapter.notifyDataSetChanged();
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_search_white_24dp)
                            .setContentTitle("New Post!")
                            .setContentText("Click to view new articles");

            Intent resultIntent = new Intent(this, MainActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                    (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);
            // first element (0) allows you to update the notification later on.
            mNotificationManager.notify(0, mBuilder.build());
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //There was an error in the communication
            //We can notify the user about it
        }
    };

}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/32283247-cd12-4454-be5d-12de5cc2a7f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to