2016-03-17 37 views
0

我已將GCM推送通知用於我的應用。一切安好。如何處理不同的GCM通知點擊

當我收到通知,當我點擊通知時,我可以移動其他activity.I傳遞url鏈接到另一個activity.but,如何處理點擊多個通知。

這裏說,當我得到5個通知,並點擊任何通知我正在轉移到其他活動,但我傳遞的鏈接是第一個通知網址。我正在使用不同的通知ID

NotificationManager notificationManager = 

(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(x 
        /*ID of notification */ 
       , notificationBuilder.build()); 
+1

這個問題如何從http://stackoverflow.com/questions/36055555/how-to-handle-gcm-notifications-when-different-gcm-notifications-receive? – Michael

+0

你能告訴我解決方案 – Sanjeev

+1

不,你不應該多次發表相同的問題。請參閱http://stackoverflow.com/help/no-one-answers – Michael

回答

0

創建PendingIntent時使用PendingIntent.FLAG_UPDATE_CURRENT。

此外,在傳遞意圖到PendingIntent之前,每次都將其操作設置爲不同。如: -

intent.setAction(Long.toString(System.currentTimeMillis())); 
+0

我應該在哪裏給這個待處理的意圖? PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* Request code */ ,intent, PendingIntent.FLAG_ONE_SHOT); – Sanjeev

+0

是的PendingIntent – maveroid

+0

讓我試試請幫我從早上卡住 – Sanjeev

0

請參見下面的代碼來處理不同的數據處理的多個通知:在上面的代碼

private void sendNotification(Bundle extras) 
{ 
    String detail = extras.getString("detail"); 
    String title = extras.getString("title"); 
    if((title==null || title.trim().length()==0) && (detail==null || detail.trim().length()==0)) 
    { 
     //title=getString(R.string.app_name); 
     return; 
    } 
    NOTIFICATION_ID = (int) (System.currentTimeMillis()/1000L); 

    Intent notificationIntent = new Intent(this, ContainerActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    String data = extras.getString("data"); 
    notificationIntent.putExtra("data",data); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 
    bigTextStyle = bigTextStyle.bigText(detail); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setContentTitle(title) 
    .setContentText(detail) 
    .setContentIntent(pendingIntent) 
    .setSmallIcon(R.drawable.app_logo) 
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo)) 
    .setWhen(System.currentTimeMillis()) 
    .setAutoCancel(true) 
    .setVibrate(new long[] { 0, 100, 200, 300 }) 
    .setPriority(NotificationCompat.PRIORITY_MAX) 
    .setStyle(bigTextStyle); 

    mBuilder.setDeleteIntent(getDeleteIntent(NOTIFICATION_ID)); 

    Set<String> pnIdsSet = ((MyAccountApplication)getApplication()).getPrefs().getStringSet("PUSH_IDS", null); 
    if(pnIdsSet==null) 
    { 
     pnIdsSet=new HashSet<String>(); 
    } 
    pnIdsSet.add(""+NOTIFICATION_ID); 
    ((MyAccountApplication)getApplication()).getPrefs().edit().putStringSet("PUSH_IDS", pnIdsSet).commit(); 

    mBuilder.setContentIntent(pendingIntent); 
    Notification n = mBuilder.build(); 

    n.flags |= Notification.FLAG_SHOW_LIGHTS; 
    n.flags |= Notification.FLAG_AUTO_CANCEL; 
    n.defaults = Notification.DEFAULT_ALL;  
    n.when = System.currentTimeMillis(); 

    mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(NOTIFICATION_ID, n); 
} 
private PendingIntent getDeleteIntent(int pnId) 
{ 
    Intent intent = new Intent(this, NotificationBroadcastReceiver.class); 
    intent.setAction("notification_cancelled"); 
    intent.putExtra("PN_ID", ""+pnId); 
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
} 

我處理通知刪除的意圖也看到每個通知具有的PendingIntent不同的通知ID。 FLAG_CANCEL_CURRENT標誌。