1 回答
TA贡献1805条经验 获得超9个赞
首先在你的Push方法中:
public void Push()
{
...
Intent intentDismiss = new Intent(this, typeof(NotivicationBroadCast));
intentDismiss.SetAction("notification_dismiss");
PendingIntent pendingIntentDismiss = PendingIntent.GetBroadcast(this, 0,
intentDismiss, PendingIntentFlags.UpdateCurrent);
Intent intentCancel = new Intent(this, typeof(NotivicationBroadCast));
intentCancel.SetAction("notification_cancel");
PendingIntent pendingIntentCancel = PendingIntent.GetBroadcast(this, 0,
intentCancel, PendingIntentFlags.UpdateCurrent);
var builder = new NotificationCompat.Builder(Activity, ChannelId)
.SetAutoCancel(true)
.SetContentTitle(Title)
.SetSmallIcon(Resource.Drawable.stop_bus)
.SetContentText(Text)
.AddAction(0, "Dont Dismiss", pendingIntentDismiss)
.AddAction(0, "Dismiss", pendingIntentCancel );
var notificationManager = NotificationManagerCompat.From(Activity);
notificationManager.Notify(41144, builder.Build());
}
在GetVehiclePositionRepeatAsync方法中:
public async void GetVehiclePositionRepeatAsync(TimeSpan interval, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested) {
var task = new Task(() =>
{
cancellationToken.Token.ThrowIfCancellationRequested();
var not = new PushNotificationGenerator(this, "Hooray!", "yay", "STOP_NOTIFICATIONS");
not.Push();
},cancellationToken.Token);
task.Start();
await Task.Delay(interval);
}
}
}
然后自定义NotivicationBroadCast:
[BroadcastReceiver]
class NotivicationBroadCast : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;
if (action.Equals("notification_dismiss"))
{
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Cancel(notificationId);
}
if (action.Equals("notification_cancel"))
{
cancellationToken.Cancel();
}
}
}
不要忘记在活动中注册接收器:
protected override void OnResume()
{
base.OnResume();
NotivicationBroadCast recieve = new NotivicationBroadCast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.AddAction("notification_dismiss");
intentFilter.AddAction("notification_cancel");
RegisterReceiver(recieve, intentFilter);
begin();
}
- 1 回答
- 0 关注
- 90 浏览
添加回答
举报