如果您使用“Google Cloud Message”通过“PendingIntent”类接收推送通知,则以下代码仅在操作栏中显示通知。
单击通知将不会创建任何活动,最后一个活动活动将恢复,从而保持当前状态而不会出现问题。
Intent notificationIntent = new Intent(this, ActBase.class); **notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);** PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Localtaxi") .setVibrate(vibrate) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setAutoCancel(true) .setOnlyAlertOnce(true) .setContentText(msg);
mBuilder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
的Ciao!
我觉得应该是[FLAG_ACTIVITY_SINGLE_TOP(http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP)。 – 2012-05-17 11:18:44
@ user942821'FLAG_ACTIVITY_SINGLE_TOP'在这里没有帮助。当ActivityC位于堆栈的顶部并且它调用'startActivity()'来启动ActivityA时,即使您设置了“FLAG_ACTIVITY_SINGLE_TOP”,它仍然会创建一个ActivityA的**新实例**。如果您尝试启动已经位于堆栈顶部的活动**,则“FLAG_ACTIVITY_SINGLE_TOP”只会阻止创建新实例**。在这种情况下,ActivityA不在栈顶,所以Android创建一个新的实例。 – 2013-03-21 10:34:34
太棒了!使用'(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP)'在给定的'活动'后清除任何历史记录。 – shkschneider 2015-06-09 15:48:14