当Android应用程序关闭时,只有一个gcm通知打开

时间:2014-11-01 04:35:54

标签: android google-cloud-messaging

我正在开发一个Android应用程序,其中iam获得超过一个gcm通知所有通知在应用程序打开时打开不同的数据但是当应用程序关闭时我得到多个通知但只有一个打开,当我点击其他通知时不会使用不同的数据重新启动活动。

    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++;

2 个答案:

答案 0 :(得分:0)

notificationManager.notify(gcmCounter, notification); 

每次更改gcmCounter的值。您可以在通知区域中一次看到多个通知。

Documentation说:

如果您的应用程序已发布具有相同ID的通知但尚未取消,则该通知将被更新的信息替换。

答案 1 :(得分:0)

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

FLAG_ACTIVITY_CLEAR_TASK清除您在点击通知时分配的先前意图和新任务是否已打开第一个通知。