2014-11-01 58 views
0

iam開發一個android應用程序,其中iam獲取多個gcm通知當應用程序處於打開狀態但所有應用程序關閉時,所有通知都以不同的數據打開但我只能獲取多個通知一個打開,當我點擊其他通知時,它不會重新啓動具有不同數據的活動。只有一個gcm通知在Android應用程序關閉時打開

String title = intent.getExtras().getString("title"); 
    String message = intent.getExtras().getString("info"); 
    String lat = intent.getExtras().getString("lat"); 
    String lng = intent.getExtras().getString("lng"); 
    Intent notificationIntent = null; 
    if(title.equals("blockage")) 
    { 
     notificationIntent = new Intent(context, ViewBlockage.class); 

    } 
    else if(title.equals("icrequest")) 
    { 
     notificationIntent = new Intent(context, IntercarAlert.class); 
     message=intent.getExtras().getString("reqMessage"); // change the message here for request message 
     notificationIntent.putExtra("reqName", intent.getExtras().getString("userName")); 
    } 
    else if(title.equals("icresponse")) 
    { 
     notificationIntent = new Intent(context, InterCarResponseAlert.class); 
    } 
    notificationIntent.putExtra("title", title); 
    notificationIntent.putExtra("message", message); 
    notificationIntent.putExtra("lat", lat); 
    notificationIntent.putExtra("lng", lng); 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
    context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String appName = context.getString(R.string.app_name); 
    // set intent so it does not start a new activity 
    //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pIntent =PendingIntent.getActivity(context, gcmCounter, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
    // update current must for every time get updated pending intent otherwise it will provide wrong intent values 
    notification.setLatestEventInfo(context, appName, message, pIntent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(gcmCounter, notification); 
    gcmCounter++; 
+0

你可以發佈你的活動代碼嗎? – 2014-11-01 04:44:55

回答

0
notificationManager.notify(gcmCounter, notification); 

這裏,每個時間變化gcmCounter的值。您可以一次在通知區域看到多個通知。

Documentation說:

如果具有相同ID的通知已經發布您的應用程序並沒有被取消,它會被更新的信息所替代。

+0

我如何解決這個問題,因爲當應用程序打開時,我可以看到所有通知與不同的數據集,但是當我收到所有通知,但只有打開一個。 – user1745334 2014-11-01 04:50:27

+0

好吧,現在我的所有通知打開,但首先我必須關閉已經打開的通知,然後打開其他如果我已經打開通知,我打開另一個什麼都沒有發生 – user1745334 2014-11-01 05:15:55

+0

隨着身份證(gcmCounter在你的情況)取消你想要的通知,休息將仍然存在。 – 2014-11-01 05:21:47

0

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

FLAG_ACTIVITY_CLEAR_TASK清除以前的意圖和當您單擊通知時分配的新任務是否已經打開第一個通知。

相關問題