Android GCM开放式应用程序,包含数据

Android GCM开放式应用程序,包含数据,android,android-intent,android-activity,google-cloud-messaging,Android,Android Intent,Android Activity,Google Cloud Messaging,当用户按推送通知打开应用程序时,我想打开一个不同的活动 此活动需要位于主活动的顶部。因此,当用户按下通知时,堆栈应为: 活动A->活动B 现在我认为他们有两种情况: 应用程序已在运行,因此已显示主要活动 应用程序未运行。未介绍主要活动 所以我的问题是:我应该如何打开应用程序,显示一个新的活动,并在其堆栈中显示主活动 下面的代码仅在应用程序已经运行时有效,因此主活动已经在堆栈中 Intent notificationIntent = new Intent(this, ExtraActivi

当用户按推送通知打开应用程序时,我想打开一个不同的活动

此活动需要位于主活动的顶部。因此,当用户按下通知时,堆栈应为:

活动A->活动B

现在我认为他们有两种情况:

应用程序已在运行,因此已显示主要活动 应用程序未运行。未介绍主要活动 所以我的问题是:我应该如何打开应用程序,显示一个新的活动,并在其堆栈中显示主活动

下面的代码仅在应用程序已经运行时有效,因此主活动已经在堆栈中

    Intent notificationIntent = new Intent(this, ExtraActivity.class);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setAction(Intent.ACTION_MAIN);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

    String appName = getResources().getString(R.string.app_name);
    int icon = R.drawable.ic_launcher;

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(icon)
    .setContentTitle(appName)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(message))
    .setContentText(message);

    mBuilder.setContentIntent(pendingIntent);

    mNotificationManager.notify(uniqueId, mBuilder.build());
试试这个

Intent intent;
manager = getPackageManager();
try {
  intent = manager.getLaunchIntentForPackage(getApplicationContext().getPackageName());
  if (intent == null)
    throw new PackageManager.NameNotFoundException();
  intent.addCategory(Intent.CATEGORY_LAUNCHER);
  intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  finish();
  startActivity(intent);
}
catch (Exception e) {
}