我想阻止通知在忙于其他活动时中断用户体验。
当用户忙于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();
如您所见,活动一次只能独立运行一次。