1

我正在使用GCMBaseIntentService。 在我的serive類中擴展了GCMBaseIntentService,我重寫了onMessage(Context,Intent)方法。 代碼如下: protected void onMessage(Context context,Intent intent)Log.i(TAG,「Received message」);從通知中打開活動-Android

//String message = getString(R.string.gcm_message); 
    String id=intent.getExtras().getString("event_id"); 
    String message=intent.getExtras().getString("title"); 
    String eventname=intent.getExtras().getString("event_name"); 


    generateNotification(getApplicationContext(), id, message, eventname); 
} 

    private static void generateNotification(Context context, String id, String message, String eventname) 
{ 
    int icon = R.drawable.ic_stat_gcm; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    //new intent 
    Intent notificationIntent = new Intent(context, EventDescription.class); 
    notificationIntent.putExtra("event_id", id);//need this id in the eventdescription activity 


    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 


    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, eventname, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 


    notificationManager.notify(0, notification); 

} 

問題是,雖然我點擊這個通知,並在Eventdescription活動,並從意圖提取額外和打印ID。它僅在第一次顯示正確的值,然後對於每個通知它始終顯示第一個值。請幫忙!

回答

0

試試這個

PendingIntent intent = PendingIntent.getActivity(context, **uniqueKey**, notificationIntent, 0); 

請注意待定意圖getActivity的requestCode應unique.So只是通過

爲每notification.You

獨特的價值可以把時間戳甚至ID您從服務器接收

。我曾經嘗試這樣做,這works.Do分享您的反饋

+0

好thnx ..我會嘗試! – gaurav414u

+0

它不工作,仍然發生同樣的事情! – gaurav414u

+0

您能夠獲取多個通知,或者您一次只檢查一個通知。還有你檢查是否String id = intent.getExtras()。getString(「event_id」);你傳遞給generateNotification的值是不同的? – curious

0
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
+0

它不工作...仍然發生同樣的事情.. – gaurav414u

+0

PendingIntent intent = PendingIntent.getActivity(context,0,notificationIntent ,** Intent.FLAG_ACTIVITY_NEW_TASK **); – avesha

0
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.notificationsfinal); 


    start=1; 
    end=15; 

    next=(Button)findViewById(R.id.nextbtn); 
    prev=(Button)findViewById(R.id.prevbtn); 
    statusview=(TextView)findViewById(R.id.status); 


    notification_list_view=(ListView)findViewById(R.id.not_listview); 
    updatestatus(); 
    getNotifications(); 

} 
private void getNotifications() 
{ 
    eventnotifierapp app=(eventnotifierapp)getApplication(); 



    id=getIntent().getExtras().getString("event_id"); 
    db=new MyDB(this); 
    } 

這是我在哪裏retreiving此事項標識

0

你以後Intent s爲相差不大,不足以我EventDescription活動,因此他們重新被回收。特別是,從系統的角度來看,更改extras並沒有形成明確的意圖。

Intent.filterEquals的文檔解釋了您需要更改以下一項或多項:操作,數據,類型,類和類別。因此,一種方法是將一些區分信息(在您的案例中是event_id)粘貼到數據URI中;您不必使用此URI進行任何操作,但它可以確保您的意圖不同。

還有更多的信息在這related question