XML 项目更新时显示通知 [英] Show notification when XML item is updated

查看:21
本文介绍了XML 项目更新时显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对通知有疑问.我想要实现的是,当来自 XML 的项目更新或发布时,应该触发通知给我一些解决这个问题的提示.

I have problem about notification. What I want to achieve is that, notification should get triggered when item from XML is updated or post give me some hint to solve this problem.

public class ReadRss extends AsyncTask <Void, Void, Void> {

    public Context context;        
    public String address ;
    public ProgressDialog progressDialog;
    public ArrayList<FeedItem>feedItems;
    public RecyclerView recyclerView;
    public URL url;

    public ReadRss(Context context,RecyclerView recyclerView,  String address) {
        this.recyclerView=recyclerView;
        this.context = context;
        progressDialog = new ProgressDialog(context ,R.style.MyTheme);
       /* progressDialog.setCancelable(false);*/
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);    // Trasparent Color  #00F0F8FF   00000000
        progressDialog.setIndeterminate(true);
        progressDialog.setIndeterminateDrawable(context.getResources()
                .getDrawable(R.drawable.progressbar_handler));
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));

        progressDialog.dismiss();
        this.address = address;


    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressDialog.dismiss();
        super.onPostExecute(aVoid);
        MyAdapter adapter=new MyAdapter(context,feedItems);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        /*recyclerView.addItemDecoration(new VerticalSpace(5));*/
        recyclerView.setAdapter(adapter);


    }

    @Override
    protected Void doInBackground(Void... params) {
        ProcessXml(Getdata());

        return null;
    }

    private void ProcessXml(Document data) {
        if (data != null) {
            feedItems=new ArrayList<>();
            Element root = data.getDocumentElement();
            Node channel = root.getChildNodes().item(1);
            NodeList items = channel.getChildNodes();
            for (int i = 0; i < items.getLength(); i++) {
                Node cureentchild = items.item(i);
                if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
                    FeedItem item=new FeedItem();
                    NodeList itemchilds = cureentchild.getChildNodes();
                    for (int j = 0; j < itemchilds.getLength(); j++) {
                        Node cureent = itemchilds.item(j);
                        if (cureent.getNodeName().equalsIgnoreCase("title")){
                            item.setTitle(cureent.getTextContent());
                        }else if (cureent.getNodeName().equalsIgnoreCase("content:encoded")){
                            item.setDescription(cureent.getTextContent());
                        }else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
                            item.setPubDate(cureent.getTextContent());
                        }else if (cureent.getNodeName().equalsIgnoreCase("link")){
                            item.setLink(cureent.getTextContent());
                        }/*else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")){
                            //this will return us thumbnail url
                            String url=cureent.getAttributes().item(0).getTextContent();
                            item.setThumbnailUrl(url);
                        }*/
                    }
                    feedItems.add(item);

                }

            }
        }else {

            feedItems.add(null);
        }
    }

    public Document Getdata() {
        try {
            url = new URL(address);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document xmlDoc = builder.parse(inputStream);
            return xmlDoc;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

我的通知代码是

Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext...");   //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);

推荐答案

我终于通过关注 获得了预定的通知这个链接

这篇关于XML 项目更新时显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆