2012-04-01 92 views
1

在我的應用程序中,我使用了C2DM。在這裏,當我收到C2DM消息/通知時,我正嘗試使用通知管理器創建通知。我可以正確地創建通知,但是當有很多未讀通知時,我無法增加未讀通知的數量(使用Notification.number)。因爲我在onReceive()函數內部創建通知對象,所以一旦控件從onReceive函數中出來,它就會被銷燬。因此,陳述在BroadcastReceiver內部創建通知:Android

public class MyC2dmReceiver extends BroadcastReceiver{ 

private Context context; 

@Override 
public void onReceive(Context context1, Intent intent) { 
this.context = context1; 

    if(intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {  
     handleRegistration(context, intent); 
} 
    else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { 
     handleMessage(context, intent); 
} 
} 

private void handleMessage(Context context, Intent intent) 
{ 
    ..... 
    ..... 
    Notification notification =  
     new Notification(R.drawable.ic_launcher,intent.getStringExtra("payload"),  
       System.currentTimeMillis());  

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults = Notification.DEFAULT_ALL; 
    notification.number += 1; 
} 
} 

已經沒用了,因爲它每次都在創建一個新的通知對象。我不能讓它(通知對象)也是一個靜態對象。如果我將其設爲靜態對象,即使在查看所有未讀通知後,notification.number也不會重置。這基本上是問題。有人能幫助我嗎?

回答

0

將未讀消息的數量存儲在數據庫,平面文件,共享首選項或其他持久性存儲中。在創建新的Notification時請閱讀該編號。

+0

是的,我認爲這是唯一的方法。我使用了sharedPreferences。謝謝 :) – Kishan 2012-04-01 21:16:26