阻止Android通知将PendingIntent Activity带到前面

时间:2017-04-05 12:00:07

标签: android android-activity notifications xamarin.android android-notifications

我想阻止通知在忙于其他活动时中断用户体验。

enter image description here

当用户忙于Activity 2并且通知进入时,Main Activity会被带到前面。

如果通知被忽略/删除,我更愿意。

通知代码:

Intent notificationIntent = new Intent(context, typeof(MainActivity));
notificationIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

PendingIntent contentIntent = PendingIntent.GetActivity(context, 99, notificationIntent, PendingIntentFlags.UpdateCurrent);

var builder = new Notification.Builder(context);
//builder.SetAutoCancel(false);
//builder.SetTicker("notification");
builder.SetContentTitle(title); //title parameter
builder.SetContentText(subtitle); //subtitle parameter
builder.SetStyle(new Notification.BigTextStyle().BigText(body)); //body parameter
builder.SetSmallIcon(Resource.Drawable.notification_icon);
//builder.SetLargeIcon();
builder.SetContentIntent(contentIntent);
//builder.SetOngoing(false);
builder.SetColor(color); //color parameter
builder.SetDefaults(NotificationDefaults.Vibrate);
builder.SetLights(Android.Graphics.Color.Blue, 1500, 1000);

var notifyManager = (NotificationManager)context.GetSystemService(Context.NotificationService);

notifyManager.Notify(id, builder.Build()); //id parameter

我如何从Main Activity转移到Activity 2

var intent = new Intent(this, typeof(Activity2));
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
StartActivity(intent);
Finish();

并返回Main Activity

var intent = new Intent(this, typeof(MainActivity));
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
StartActivity(intent);
Finish();

如您所见,活动一次只能独立运行一次。

0 个答案:

没有答案