我正在构建一个单声道 android 应用程序,它接收来自 GCM 的通知并在用户单击通知时打开一个活动。
当通知创建的活动实例已经存在并且是应用程序中的当前活动活动时,就会出现问题。单击通知后,将在应用程序中创建一个重复的活动。这个问题很微妙,因为新的重复活动在前台打开并且看起来与以前的活动相同,但是当用户单击后退按钮时,重复的活动被杀死,但以前的活动仍然存在,这意味着用户必须单击后退按钮两次。
以下是用于生成通知并在点击时创建活动的当前代码。我希望该过程类似于,如果存在活动,则打开现有活动,否则开始新活动。感谢任何帮助谢谢。
void createNotification(string title, string desc)
{
//Create notification
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
//Create an intent to show ui
var uiIntent = new Intent(this, typeof(Messaging));
//Create the notification
var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title);
notification.Defaults = NotificationDefaults.Sound;
//Auto cancel will remove the notification once the user touches it
notification.Flags = NotificationFlags.AutoCancel;
//Set the notification info
//we use the pending intent, passing our ui intent over which will get called
//when the notification is tapped.
notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));
//Show the notification
notificationManager.Notify(1, notification);
}