我正在使用NotificationCompat.Builder
显示通知。我希望当用户单击通知时,它将切换到正在运行的活动,而不是固定的活动。
假设我有4个活动,我在第2个活动中,向用户显示一些通知,然后移至第4个活动,然后按下主页按钮。现在通知仍然存在。当用户单击该按钮时,它应移至第四个活动。
如何检测到?这是我到目前为止完成的代码。
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new NotificationCompat.Builder(this, 0)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(Title)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
答案 0 :(得分:0)
我得到了答案,只需要替换PendingIntent
。这是完整的代码
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
//var pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new NotificationCompat.Builder(this, 0)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(Title)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(GetRunningIntent());
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
PendingIntent GetRunningIntent()
{
var intent = PackageManager.GetLaunchIntentForPackage(PackageName)
.SetPackage(null)
.SetFlags(ActivityFlags.BroughtToFront);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, 0);
return pendingIntent;
}