0

,甚至沒有新的通知,改變了我有2份額外直到我終止服務

1-A的活動序列組成的應用程序。 (登錄,然後儀表板,然後一個提要)。 2-由推送服務器驅動的後臺服務。

每次服務接收的推送時間,創建一個具有未決的意圖此意圖將來自推送服務器例如收到的字串額外的通知:在安裝後增加了一個字符「N」或「R」

現在服務應用程序,並在第一次運行後,extras被正確發送,但隨後每隔一段時間,服務器通過intent發送消息給應用程序,額外服務不會改變,即

如果第一個消息發送「N」,則所有後續的消息都會發送「N」。

由於上面的描述太複雜,我給你我的代碼片段:

這是在創建一個具有未決的意圖

Notification n = new Notification();    
    n.flags |= Notification.FLAG_SHOW_LIGHTS; 
    n.flags |= Notification.FLAG_AUTO_CANCEL; 
    n.defaults = Notification.DEFAULT_ALL;  
    n.when = System.currentTimeMillis(); 
    // The following line displays N or R depending on the message received . 
    Log.d("Received Type: ",text.substr(0,1); // Example "N" from: "N|Notification Text" 

    n.icon = R.drawable.notification_icon; 
    Intent i = new Intent(this, MyActivity.class); 
    i.putExtra("type", text.substr(0,1); 
i.putExtra("text", text.substr(2); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, i , 0); 
    n.setLatestEventInfo(this, NOTIF_TITLE, "Notification: " + text.substring(2), pi); 

的通知現在,這是服務的代碼從意向接收數據的活動

super.onCreate(savedInstanceState); 
setContentView(R.layout.mylayout); 
activeWebview = (WebView) findViewById(R.id.webview); 

Intent sender=getIntent(); 
type = sender.getStringExtra("type"); // Assume this is defined somewhere 
text = sender.getStringExtra("text"); // Assume this is defined somewhere 
    Log.d("Type received:" , type); // This everytime displays "N" 

我的問題是,在意向發送的附加功能都是一樣的,每次新的通知爲r只要我終止應用程序和服務並重新啓動,它就不會改變。

,彷彿這是什麼地方保存在內存中,我需要清理

回答

1

您需要添加標誌在掛起的意圖:

PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT 

PendingIntent pi = PendingIntent.getActivity(this, 0 , i , PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT); 

否則,演員不會得到更新。 http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT

+0

它適用於我.... – 2015-02-01 21:53:21

+1

M很高興它幫助:) – 2015-02-02 00:27:18

1

線輪我想解決的辦法是在這一部分:

的PendingIntent PI = PendingIntent.getActivity(這一點,>>>> 0 < < < < <,i,0);

這0需要改變每一個不同類型的通知