2016-08-02 67 views
1

我有關於通知的問題。我想要實現的是,當來自XML的項目被更新時,通知應該被觸發,或者給我一些提示來解決這個問題。當XML項目更新時顯示通知

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); 
+0

你的問題有些寬泛獲得預定的通知。嘗試縮小它。你想顯示一個'Notification'還是你想檢查一個遠程XML是否已經改變?或者是其他東西? – Sufian

+1

在服務器端代碼中進行處理(檢查xml是否已更新)。使用推送通知來通知您的應用程序並將其顯示在通知欄中 –

回答